Created
July 1, 2025 14:38
-
-
Save mattiamanzati/469429d8ca90a7d321fadaf4f29ab117 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AiError: AnthropicLanguageModel.generateText: An error occurred | |
at cause (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@effect+platf_ec8b015952fd411ff0df445882568c36/node_modules/@effect/ai-anthropic/src/AnthropicLanguageModel.ts:187:37) | |
at cause (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/core.ts:910:24) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1186:44) | |
at body (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Utils.ts:786:14) | |
at FiberRuntime.Failure (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1186:20) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1380:53) | |
at f (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/tracer.ts:101:19) | |
at runLoop (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1370:34) | |
at evaluateEffect (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:936:27) | |
at evaluateMessageWhileSuspended (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:904:14) | |
at AiLanguageModel.generateText { | |
name: '(FiberFailure) AiError', | |
[Symbol(effect/Runtime/FiberFailure)]: Symbol(effect/Runtime/FiberFailure), | |
[Symbol(effect/Runtime/FiberFailure/Cause)]: { | |
_tag: 'Fail', | |
error: AiError: AnthropicLanguageModel.generateText: An error occurred | |
at cause (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@effect+platf_ec8b015952fd411ff0df445882568c36/node_modules/@effect/ai-anthropic/src/AnthropicLanguageModel.ts:187:37) | |
at cause (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/core.ts:910:24) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1186:44) | |
at body (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Utils.ts:786:14) | |
at FiberRuntime.Failure (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1186:20) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1380:53) | |
at f (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/tracer.ts:101:19) | |
at runLoop (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1370:34) | |
at evaluateEffect (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:936:27) | |
at evaluateMessageWhileSuspended (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:904:14) { | |
module: 'AnthropicLanguageModel', | |
method: 'generateText', | |
description: 'An error occurred', | |
_tag: 'AiError', | |
[Symbol(@effect/ai/AiError)]: Symbol(@effect/ai/AiError), | |
[cause]: ParseError: (Message (Encoded side) <-> Message) | |
└─ Encoded side transformation failure | |
└─ Message (Encoded side) | |
└─ ["usage"] | |
└─ { readonly cache_creation_input_tokens: Int & greaterThanOrEqualTo(0) | null | null; readonly cache_read_input_tokens: Int & greaterThanOrEqualTo(0) | null | null; readonly input_tokens: Int & greaterThanOrEqualTo(0); readonly output_tokens: Int & greaterThanOrEqualTo(0); readonly server_tool_use: { readonly web_search_requests: Int & greaterThanOrEqualTo(0) } | null | null; readonly service_tier: "standard" | "priority" | "batch" | null | null } | |
└─ ["server_tool_use"] | |
└─ is missing | |
at issue (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/ParseResult.ts:266:62) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Either.ts:439:25) | |
at Module.<anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Function.ts:245:18) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/ParseResult.ts:374:12) | |
at Module.<anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Function.ts:245:18) | |
at EffectPrimitive.effect_instruction_i1 (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Schema.ts:565:17) | |
at <anonymous> (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:152:36) | |
at body (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/Utils.ts:786:14) | |
at Object.OnSuccess (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:152:12) | |
at FiberRuntime.Success (/Users/mattiamanzati/Desktop/timmeee/node_modules/.pnpm/[email protected]/node_modules/effect/src/internal/fiberRuntime.ts:1171:37) { | |
issue: Transformation { | |
ast: Transformation { | |
from: [TypeLiteral], | |
to: [Declaration], | |
transformation: [FinalTransformation], | |
annotations: [Object], | |
_tag: 'Transformation' | |
}, | |
actual: { | |
id: 'msg_01XzLyzpxfY7nWZLkWuPBX2E', | |
type: 'message', | |
role: 'assistant', | |
model: 'claude-sonnet-4-20250514', | |
content: [Array], | |
stop_reason: 'tool_use', | |
stop_sequence: null, | |
usage: [Object] | |
}, | |
kind: 'Encoded', | |
issue: Composite { | |
ast: [TypeLiteral], | |
actual: [Object], | |
issues: [Pointer], | |
output: [Object], | |
_tag: 'Composite' | |
}, | |
_tag: 'Transformation' | |
}, | |
_tag: 'ParseError', | |
[Symbol(effect/Schema/ParseErrorTypeId)]: Symbol(effect/Schema/ParseErrorTypeId) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AiLanguageModel, AiTool, AiToolkit } from "@effect/ai" | |
import { AnthropicClient, AnthropicLanguageModel } from "@effect/ai-anthropic" | |
import { HttpClient, HttpClientRequest, HttpClientResponse } from "@effect/platform" | |
import { NodeHttpClient } from "@effect/platform-node" | |
import { Array, Console, Effect, Layer, Redacted, Schema } from "effect" | |
class DadJoke extends Schema.Class<DadJoke>("DadJoke")({ | |
id: Schema.String, | |
joke: Schema.String | |
}) {} | |
class SearchResponse extends Schema.Class<SearchResponse>("SearchResponse")({ | |
results: Schema.Array(DadJoke) | |
}) {} | |
class ICanHazDadJoke extends Effect.Service<ICanHazDadJoke>()("ICanHazDadJoke", { | |
dependencies: [NodeHttpClient.layerUndici], | |
effect: Effect.gen(function*() { | |
const httpClient = yield* HttpClient.HttpClient | |
const httpClientOk = httpClient.pipe( | |
HttpClient.filterStatusOk, | |
HttpClient.mapRequest(HttpClientRequest.prependUrl("https://icanhazdadjoke.com")) | |
) | |
const search = Effect.fn("ICanHazDadJoke.search")( | |
function*(searchTerm: string) { | |
return yield* httpClientOk.get("/search", { | |
acceptJson: true, | |
urlParams: { searchTerm } | |
}).pipe( | |
Effect.flatMap(HttpClientResponse.schemaBodyJson(SearchResponse)), | |
Effect.flatMap(({ results }) => Array.head(results)), | |
Effect.map((joke) => joke.joke), | |
Effect.scoped, | |
Effect.orDie | |
) | |
} | |
) | |
return { | |
search | |
} as const | |
}) | |
}) {} | |
class DadJokeTools extends AiToolkit.make( | |
AiTool.make("GetDadJoke", { | |
description: "Get a hilarious dad joke from the ICanHazDadJoke API", | |
success: Schema.String, | |
failure: Schema.Never, | |
parameters: { | |
searchTerm: Schema.String.annotations({ | |
description: "The search term to use to find dad jokes" | |
}) | |
} | |
}) | |
) {} | |
const DadJokeToolHandlers = DadJokeTools.toLayer( | |
Effect.gen(function*() { | |
const icanhazdadjoke = yield* ICanHazDadJoke | |
return { | |
GetDadJoke: ({ searchTerm }) => icanhazdadjoke.search(searchTerm) | |
} | |
}) | |
).pipe(Layer.provide(ICanHazDadJoke.Default)) | |
const program = AiLanguageModel.generateText({ | |
prompt: "Generate a dad joke about pirates", | |
toolkit: DadJokeTools | |
}).pipe( | |
Effect.flatMap((response) => Console.log(response.text)), | |
Effect.provide(AnthropicLanguageModel.model("claude-4-sonnet-20250514")) | |
) | |
const OpenAi = AnthropicClient.layer({ | |
apiKey: Redacted.make( | |
"---------" | |
) | |
}).pipe(Layer.provide(NodeHttpClient.layerUndici)) | |
program.pipe( | |
Effect.provide([OpenAi, DadJokeToolHandlers]), | |
Effect.runPromise | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment