effect-assistant 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/internal/loop.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ApprovalRequested, ModelOutput, ModelPart, ToolResult, ToolStarted } from "../AssistantEvent.js";
|
|
2
2
|
import { unansweredCalls } from "./history.js";
|
|
3
|
-
import {
|
|
3
|
+
import { annotateModelCall, toolOutput, toolSpan } from "./telemetry.js";
|
|
4
4
|
import { empty, failed } from "./toolkit.js";
|
|
5
5
|
import { isTurnEnding, isUserMessage } from "./tools.js";
|
|
6
6
|
import * as Context from "effect/Context";
|
|
@@ -129,7 +129,7 @@ const step = (options, then) => {
|
|
|
129
129
|
const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history]);
|
|
130
130
|
const name = yield* Effect.serviceOption(Model.ModelName);
|
|
131
131
|
const started = yield* Clock.currentTimeMillis;
|
|
132
|
-
const response = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(
|
|
132
|
+
const response = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(annotateModelCall(instructions.attributes));
|
|
133
133
|
const metadata = response.content.find((part) => part.type === "response-metadata");
|
|
134
134
|
const output = ModelOutput.make({
|
|
135
135
|
parts: modelParts(response.content),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop.js","names":["AssistantEvent.ModelPart","Telemetry.toolOutput","Telemetry.toolSpan","Toolkits.failed","Toolkits.empty","History.unansweredCalls","Telemetry.chatSpan"],"sources":["../../src/internal/loop.ts"],"sourcesContent":["import type * as AiError from \"effect/ai/AiError\"\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport type * as Response from \"effect/ai/Response\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as Instructions from \"../Instructions.ts\"\nimport * as History from \"./history.ts\"\nimport * as Telemetry from \"./telemetry.ts\"\nimport * as Toolkits from \"./toolkit.ts\"\nimport { isTurnEnding, isUserMessage } from \"./tools.ts\"\n\n/** @internal */\nexport type Event =\n | AssistantEvent.ModelOutput\n | AssistantEvent.ToolStarted\n | AssistantEvent.ToolResult\n | AssistantEvent.ApprovalRequested\n\n// Only a turn's loop can wait for the user's approval, and runs the calls they approved. A turn's specialists inherit\n// its context, so their runs set it back to none.\n/** @internal */\nexport const TurnApprovals = Context.Reference<\n Option.Option<{ readonly approved: Effect.Effect<ReadonlyArray<AssistantEvent.ToolCallPart>> }>\n>(\"effect-assistant/TurnApprovals\", { defaultValue: Option.none })\n\n// The assistant's own tools with the turn's services, so an app's own loop in a turn can message the user. A\n// specialist's run has none.\n/** @internal */\nexport const TurnTools = Context.Reference<Option.Option<Toolkits.Tools>>(\"effect-assistant/TurnTools\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport interface ChatHistory {\n readonly messages: ReadonlyArray<Prompt.Message>\n readonly throughSequence: number\n}\n\nconst readHistory = (read: ReadonlyArray<Prompt.Message> | ChatHistory) =>\n \"messages\" in read\n ? { messages: read.messages, throughSequence: read.throughSequence }\n : { messages: read, throughSequence: undefined }\n\n// `AssistantLoop` takes native toolkits; turns and specialist runs come here with their tools already merged.\n/** @internal */\nexport interface StepOptions<R = never> {\n readonly instructions: string | Instructions.Resolved\n readonly toolkit?: Toolkits.Tools<R> | undefined\n readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>\n readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined\n}\n\n/** @internal */\nexport interface Options<R = never> extends StepOptions<R> {\n readonly maxSteps?: number | undefined\n}\n\n// Its member docs show through `AssistantLoop.StepResult`.\n/** @internal */\nexport interface StepResult {\n /** The model's output; unset when the step only ran calls a crash cut off. */\n readonly output: AssistantEvent.ModelOutput | undefined\n /**\n * Whether the model is done: it made no tool calls, one of its calls waits for the user's\n * approval, or it ended its turn (`wait`, or a message with `wait: true`) and none of the step's\n * calls failed.\n */\n readonly done: boolean\n /**\n * The history's length with this step's output and results: a longer one got something new meanwhile.\n */\n readonly historyLength: number\n}\n\n// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which\n// calls wait for approval: the model call would ask for approvals of its own.\nconst requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, Tool.Any>> => ({\n tools: Object.fromEntries(\n definitions.map((tool) => [\n tool.name,\n tool.needsApproval === undefined || tool.needsApproval === false ? tool : tool.setNeedsApproval(false)\n ])\n ),\n handle: () => Effect.succeed(Stream.empty)\n})\n\n// Tool call resolution stays on with a toolkit that runs nothing: with it off, a call whose params don't fit its\n// tool's schema fails the whole model call, and here the model reads why instead.\nconst generate = (\n prompt: Prompt.Prompt,\n definitions: ReadonlyArray<Tool.Any>,\n toolChoice: LanguageModel.ToolChoice<string> | undefined\n): Effect.Effect<\n LanguageModel.GenerateTextResponse<Record<string, Tool.Any>, \"opaque\">,\n AiError.AiError,\n LanguageModel.LanguageModel\n> => LanguageModel.generateText({ prompt, toolkit: requestToolkit(definitions), toolChoice })\n\nconst decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)\n\nconst modelParts = (content: ReadonlyArray<Response.AllParts<Record<string, Tool.Any>>>) =>\n content.flatMap((part) =>\n Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === \"tool-call\" && decoded.providerExecuted))\n )\n\nconst toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>\n parts.flatMap((part) => (part.type === \"tool-call\" ? [part] : []))\n\nconst runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {\n const result = yield* Effect.gen(function* () {\n const result = yield* tools.run(call)\n\n yield* Telemetry.toolOutput(result.result)\n\n return result\n }).pipe(Telemetry.toolSpan(call))\n\n return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })\n})\n\n// A call that needs approval waits for the user in a turn. A specialist's run can't ask them, so its call fails.\nconst awaitApproval = Effect.fnUntraced(function* (call: AssistantEvent.ToolCallPart) {\n if (Option.isNone(yield* TurnApprovals)) {\n return AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `${call.name} needs the user's approval, and only the assistant can ask for it. Don't run it: say in your answer what needs approving, with the details.`,\n \"NeedsApproval\"\n )\n })\n }\n\n return AssistantEvent.ApprovalRequested.make({ callId: call.id, tool: call.name, params: call.params })\n})\n\ninterface Pending {\n readonly call: AssistantEvent.ToolCallPart\n /** The user approved it: it runs without asking again. */\n readonly approved: boolean\n}\n\ninterface Ran {\n /** One of the calls failed, so the model has to read the results. */\n readonly failed: boolean\n /** One of the calls waits for the user's approval, so the turn ends. */\n readonly waiting: boolean\n}\n\n// The approval shows the call, not what it does: the user learns that from the assistant's message beside it.\nconst unexplained = (call: AssistantEvent.ToolCallPart) =>\n AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `Not sent for approval: the user would see buttons without knowing what they approve. In one step, send them a message with send_user_message saying what ${call.name} will do (what, to whom, how many), and call ${call.name} again.`,\n \"ApprovalNeedsMessage\"\n )\n })\n\n// Every `ToolStarted` is stored before any call runs; a call that waits for approval doesn't start.\nconst runCalls = <R, S, E, R2>(\n tools: Toolkits.Tools<R>,\n pending: ReadonlyArray<Pending>,\n history: ReadonlyArray<Prompt.Message>,\n then: (ran: Ran) => Stream.Stream<Step<never, S>, E, R2>\n): Stream.Stream<Step<Event, S>, E, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const waits = yield* Effect.forEach(pending, ({ call, approved }) =>\n approved ? Effect.succeed(false) : tools.needsApproval(call, history)\n )\n\n const started = pending.flatMap(({ call }, i) =>\n waits[i] === true ? [] : [value(AssistantEvent.ToolStarted.make({ callId: call.id, tool: call.name }))]\n )\n\n return Stream.fromIterable(started).pipe(\n Stream.concat(\n Stream.unwrap(\n Effect.gen(function* () {\n const explained = pending.some(({ call }) => isUserMessage(call))\n\n const done = yield* Effect.forEach(\n pending,\n ({ call }, i): Effect.Effect<Event, never, R> =>\n waits[i] !== true\n ? runCall(tools, call)\n : explained\n ? awaitApproval(call)\n : Effect.succeed(unexplained(call)),\n { concurrency: \"unbounded\" }\n )\n\n return Stream.fromIterable(done.map(value)).pipe(\n Stream.concat(\n then({\n failed: done.some(isFailure),\n waiting: done.some((event) => Predicate.isTagged(event, \"ApprovalRequested\"))\n })\n )\n )\n })\n )\n )\n )\n })\n )\n\nconst isFailure = (event: Event) => Predicate.isTagged(event, \"ToolResult\") && event.outcome === \"failure\"\n\n/** @internal */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: Record<never, never>\n}>\n\ninterface StepDefinition extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Steps = Data.taggedEnum<StepDefinition>()\n\n/** @internal */\nexport const value = <A>(value: A): Step<A, never> => Steps.Value({ value })\n\n/** @internal */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Next({ state }))\n\n/** @internal */\n// oxlint-disable-next-line no-unnecessary-type-parameters -- `stop<number>()` types a loop's state where inference can't.\nexport const stop = <S = never>(): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Stop())\n\nconst isValue = <A, S>(step: Step<A, S>): step is Data.TaggedEnum.Value<Step<A, S>, \"Value\"> =>\n Predicate.isTagged(step, \"Value\")\n\n// A body that ends without `next` or `stop` stops too.\n/** @internal */\nexport const loop = <S, A, E, R>(\n initial: S,\n body: (state: S) => Stream.Stream<Step<A, S>, E, R>\n): Stream.Stream<A, E, R> => {\n const go = (state: S): Stream.Stream<A, E, R> =>\n Stream.suspend(() => {\n let after: Option.Option<S> = Option.none()\n\n return body(state).pipe(\n Stream.tap((step) =>\n Effect.sync(() => (after = Predicate.isTagged(step, \"Next\") ? Option.some(step.state) : after))\n ),\n Stream.filter(isValue),\n Stream.map((step) => step.value),\n Stream.concat(Stream.suspend(() => Option.match(after, { onNone: () => Stream.empty, onSome: go })))\n )\n })\n\n return go(initial)\n}\n\n/** @internal */\nexport const step = <R, S, E2 = never, R2 = never>(\n options: StepOptions<R>,\n then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>\n): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | R | R2> => {\n const toolkit = options.toolkit ?? Toolkits.empty\n const instructions = Predicate.isString(options.instructions) ? { text: options.instructions } : options.instructions\n\n const after = (result: StepResult) => Stream.suspend(() => then(result))\n\n return Stream.unwrap(\n Effect.gen(function* () {\n const { messages: history, throughSequence } = readHistory(yield* options.history)\n // Calls a crash cut off, with no result: they run again, with the same ids, before the model goes on.\n const cutOff = History.unansweredCalls(history)\n const approvals = yield* TurnApprovals\n\n // Calls the user approved: they run as the model made them, with the same ids, before the model goes on. Not in\n // a loop without their tools.\n const approved = Option.isNone(approvals)\n ? []\n : (yield* approvals.value.approved).filter(\n (call) =>\n toolkit.definitions.some((tool) => tool.name === call.name) && !cutOff.some((cut) => cut.id === call.id)\n )\n\n if (cutOff.length > 0 || approved.length > 0) {\n const pending = [\n ...cutOff.map((call) => ({ call, approved: false })),\n ...approved.map((call) => ({ call, approved: true }))\n ]\n\n return runCalls(toolkit, pending, history, ({ waiting }) =>\n after({ output: undefined, done: waiting, historyLength: history.length + pending.length })\n )\n }\n\n const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history])\n const name = yield* Effect.serviceOption(Model.ModelName)\n const started = yield* Clock.currentTimeMillis\n\n const response = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(\n Telemetry.chatSpan(prompt, instructions.attributes)\n )\n\n const metadata = response.content.find((part) => part.type === \"response-metadata\")\n\n const output = AssistantEvent.ModelOutput.make({\n parts: modelParts(response.content),\n seenThroughSequence: throughSequence,\n model: metadata?.modelId ?? Option.getOrUndefined(name),\n responseId: metadata?.id,\n usage: response.usage,\n durationMillis: (yield* Clock.currentTimeMillis) - started\n })\n\n const calls = toolCalls(output.parts)\n const historyLength = history.length + Prompt.fromResponseParts(output.parts).content.length + calls.length\n\n // A failed call doesn't end the turn: a message that failed never reached the user, so the model reads why. A call\n // that waits for approval does: the turn waits for the user.\n return Stream.make(value(output)).pipe(\n Stream.concat(\n runCalls(\n toolkit,\n calls.map((call) => ({ call, approved: false })),\n history,\n ({ failed, waiting }) =>\n after({\n output,\n done: calls.length === 0 || waiting || (!failed && calls.some(isTurnEnding)),\n historyLength\n })\n )\n )\n )\n })\n )\n}\n\n/** @internal */\nexport const run = <R = never>(\n options: Options<R>\n): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | R> => {\n const maxSteps = options.maxSteps ?? 16\n\n // Done: stop, unless something arrived while the model was working; then it reads that first.\n const afterDone = (steps: number, historyLength: number) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const read = yield* options.history\n\n return readHistory(read).messages.length > historyLength ? next(steps) : stop<number>()\n })\n )\n\n return loop(0, (steps: number) =>\n step(options, (result) => {\n const taken = result.output === undefined ? steps : steps + 1\n\n if (taken >= maxSteps) return stop<number>()\n\n return result.done ? afterDone(taken, result.historyLength) : next(taken)\n })\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgCA,MAAa,gBAAgB,QAAQ,UAEnC,kCAAkC,EAAE,cAAc,OAAO,KAAK,CAAC;;AAKjE,MAAa,YAAY,QAAQ,UAAyC,8BAA8B,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,MAAM,MAAM,CAAC,CAAC,CAAC,KAChC,OAAO,OACL,SACE,SACA,MAAM,KAAK,UAAU;GAAE;GAAM,UAAU;EAAM,EAAE,GAC/C,UACC,EAAE,QAAQ,cACT,MAAM;GACJ;GACA,MAAM,MAAM,WAAW,KAAK,WAAY,CAAC,UAAU,MAAM,KAAK,YAAY;GAC1E;EACF,CAAC,CACL,CACF,CACF;CACF,CAAC,CACH;AACF;;AAGA,MAAa,OACX,YAC2E;CAC3E,MAAM,WAAW,QAAQ,YAAY;CAGrC,MAAM,aAAa,OAAe,kBAChC,OAAO,OACL,OAAO,IAAI,aAAa;EACtB,MAAM,OAAO,OAAO,QAAQ;EAE5B,OAAO,YAAY,IAAI,CAAC,CAAC,SAAS,SAAS,gBAAgB,KAAK,KAAK,IAAI,KAAa;CACxF,CAAC,CACH;CAEF,OAAO,KAAK,IAAI,UACd,KAAK,UAAU,WAAW;EACxB,MAAM,QAAQ,OAAO,WAAW,KAAA,IAAY,QAAQ,QAAQ;EAE5D,IAAI,SAAS,UAAU,OAAO,KAAa;EAE3C,OAAO,OAAO,OAAO,UAAU,OAAO,OAAO,aAAa,IAAI,KAAK,KAAK;CAC1E,CAAC,CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"loop.js","names":["AssistantEvent.ModelPart","Telemetry.toolOutput","Telemetry.toolSpan","Toolkits.failed","Toolkits.empty","History.unansweredCalls","Telemetry.annotateModelCall"],"sources":["../../src/internal/loop.ts"],"sourcesContent":["import type * as AiError from \"effect/ai/AiError\"\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport type * as Response from \"effect/ai/Response\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as Instructions from \"../Instructions.ts\"\nimport * as History from \"./history.ts\"\nimport * as Telemetry from \"./telemetry.ts\"\nimport * as Toolkits from \"./toolkit.ts\"\nimport { isTurnEnding, isUserMessage } from \"./tools.ts\"\n\n/** @internal */\nexport type Event =\n | AssistantEvent.ModelOutput\n | AssistantEvent.ToolStarted\n | AssistantEvent.ToolResult\n | AssistantEvent.ApprovalRequested\n\n// Only a turn's loop can wait for the user's approval, and runs the calls they approved. A turn's specialists inherit\n// its context, so their runs set it back to none.\n/** @internal */\nexport const TurnApprovals = Context.Reference<\n Option.Option<{ readonly approved: Effect.Effect<ReadonlyArray<AssistantEvent.ToolCallPart>> }>\n>(\"effect-assistant/TurnApprovals\", { defaultValue: Option.none })\n\n// The assistant's own tools with the turn's services, so an app's own loop in a turn can message the user. A\n// specialist's run has none.\n/** @internal */\nexport const TurnTools = Context.Reference<Option.Option<Toolkits.Tools>>(\"effect-assistant/TurnTools\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport interface ChatHistory {\n readonly messages: ReadonlyArray<Prompt.Message>\n readonly throughSequence: number\n}\n\nconst readHistory = (read: ReadonlyArray<Prompt.Message> | ChatHistory) =>\n \"messages\" in read\n ? { messages: read.messages, throughSequence: read.throughSequence }\n : { messages: read, throughSequence: undefined }\n\n// `AssistantLoop` takes native toolkits; turns and specialist runs come here with their tools already merged.\n/** @internal */\nexport interface StepOptions<R = never> {\n readonly instructions: string | Instructions.Resolved\n readonly toolkit?: Toolkits.Tools<R> | undefined\n readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>\n readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined\n}\n\n/** @internal */\nexport interface Options<R = never> extends StepOptions<R> {\n readonly maxSteps?: number | undefined\n}\n\n// Its member docs show through `AssistantLoop.StepResult`.\n/** @internal */\nexport interface StepResult {\n /** The model's output; unset when the step only ran calls a crash cut off. */\n readonly output: AssistantEvent.ModelOutput | undefined\n /**\n * Whether the model is done: it made no tool calls, one of its calls waits for the user's\n * approval, or it ended its turn (`wait`, or a message with `wait: true`) and none of the step's\n * calls failed.\n */\n readonly done: boolean\n /**\n * The history's length with this step's output and results: a longer one got something new meanwhile.\n */\n readonly historyLength: number\n}\n\n// The loop runs the calls itself, after storing each as started, so the model call runs none. It also decides which\n// calls wait for approval: the model call would ask for approvals of its own.\nconst requestToolkit = (definitions: ReadonlyArray<Tool.Any>): Toolkit.WithHandler<Record<string, Tool.Any>> => ({\n tools: Object.fromEntries(\n definitions.map((tool) => [\n tool.name,\n tool.needsApproval === undefined || tool.needsApproval === false ? tool : tool.setNeedsApproval(false)\n ])\n ),\n handle: () => Effect.succeed(Stream.empty)\n})\n\n// Tool call resolution stays on with a toolkit that runs nothing: with it off, a call whose params don't fit its\n// tool's schema fails the whole model call, and here the model reads why instead.\nconst generate = (\n prompt: Prompt.Prompt,\n definitions: ReadonlyArray<Tool.Any>,\n toolChoice: LanguageModel.ToolChoice<string> | undefined\n): Effect.Effect<\n LanguageModel.GenerateTextResponse<Record<string, Tool.Any>, \"opaque\">,\n AiError.AiError,\n LanguageModel.LanguageModel\n> => LanguageModel.generateText({ prompt, toolkit: requestToolkit(definitions), toolChoice })\n\nconst decodePart = Schema.decodeUnknownOption(AssistantEvent.ModelPart)\n\nconst modelParts = (content: ReadonlyArray<Response.AllParts<Record<string, Tool.Any>>>) =>\n content.flatMap((part) =>\n Option.toArray(decodePart(part)).filter((decoded) => !(decoded.type === \"tool-call\" && decoded.providerExecuted))\n )\n\nconst toolCalls = (parts: ReadonlyArray<AssistantEvent.ModelPart>): ReadonlyArray<AssistantEvent.ToolCallPart> =>\n parts.flatMap((part) => (part.type === \"tool-call\" ? [part] : []))\n\nconst runCall = Effect.fnUntraced(function* <R>(tools: Toolkits.Tools<R>, call: AssistantEvent.ToolCallPart) {\n const result = yield* Effect.gen(function* () {\n const result = yield* tools.run(call)\n\n yield* Telemetry.toolOutput(result.result)\n\n return result\n }).pipe(Telemetry.toolSpan(call))\n\n return AssistantEvent.ToolResult.make({ callId: call.id, tool: call.name, ...result })\n})\n\n// A call that needs approval waits for the user in a turn. A specialist's run can't ask them, so its call fails.\nconst awaitApproval = Effect.fnUntraced(function* (call: AssistantEvent.ToolCallPart) {\n if (Option.isNone(yield* TurnApprovals)) {\n return AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `${call.name} needs the user's approval, and only the assistant can ask for it. Don't run it: say in your answer what needs approving, with the details.`,\n \"NeedsApproval\"\n )\n })\n }\n\n return AssistantEvent.ApprovalRequested.make({ callId: call.id, tool: call.name, params: call.params })\n})\n\ninterface Pending {\n readonly call: AssistantEvent.ToolCallPart\n /** The user approved it: it runs without asking again. */\n readonly approved: boolean\n}\n\ninterface Ran {\n /** One of the calls failed, so the model has to read the results. */\n readonly failed: boolean\n /** One of the calls waits for the user's approval, so the turn ends. */\n readonly waiting: boolean\n}\n\n// The approval shows the call, not what it does: the user learns that from the assistant's message beside it.\nconst unexplained = (call: AssistantEvent.ToolCallPart) =>\n AssistantEvent.ToolResult.make({\n callId: call.id,\n tool: call.name,\n ...Toolkits.failed(\n `Not sent for approval: the user would see buttons without knowing what they approve. In one step, send them a message with send_user_message saying what ${call.name} will do (what, to whom, how many), and call ${call.name} again.`,\n \"ApprovalNeedsMessage\"\n )\n })\n\n// Every `ToolStarted` is stored before any call runs; a call that waits for approval doesn't start.\nconst runCalls = <R, S, E, R2>(\n tools: Toolkits.Tools<R>,\n pending: ReadonlyArray<Pending>,\n history: ReadonlyArray<Prompt.Message>,\n then: (ran: Ran) => Stream.Stream<Step<never, S>, E, R2>\n): Stream.Stream<Step<Event, S>, E, R | R2> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const waits = yield* Effect.forEach(pending, ({ call, approved }) =>\n approved ? Effect.succeed(false) : tools.needsApproval(call, history)\n )\n\n const started = pending.flatMap(({ call }, i) =>\n waits[i] === true ? [] : [value(AssistantEvent.ToolStarted.make({ callId: call.id, tool: call.name }))]\n )\n\n return Stream.fromIterable(started).pipe(\n Stream.concat(\n Stream.unwrap(\n Effect.gen(function* () {\n const explained = pending.some(({ call }) => isUserMessage(call))\n\n const done = yield* Effect.forEach(\n pending,\n ({ call }, i): Effect.Effect<Event, never, R> =>\n waits[i] !== true\n ? runCall(tools, call)\n : explained\n ? awaitApproval(call)\n : Effect.succeed(unexplained(call)),\n { concurrency: \"unbounded\" }\n )\n\n return Stream.fromIterable(done.map(value)).pipe(\n Stream.concat(\n then({\n failed: done.some(isFailure),\n waiting: done.some((event) => Predicate.isTagged(event, \"ApprovalRequested\"))\n })\n )\n )\n })\n )\n )\n )\n })\n )\n\nconst isFailure = (event: Event) => Predicate.isTagged(event, \"ToolResult\") && event.outcome === \"failure\"\n\n/** @internal */\nexport type Step<A, S> = Data.TaggedEnum<{\n Value: { readonly value: A }\n Next: { readonly state: S }\n Stop: Record<never, never>\n}>\n\ninterface StepDefinition extends Data.TaggedEnum.WithGenerics<2> {\n readonly taggedEnum: Step<this[\"A\"], this[\"B\"]>\n}\n\nconst Steps = Data.taggedEnum<StepDefinition>()\n\n/** @internal */\nexport const value = <A>(value: A): Step<A, never> => Steps.Value({ value })\n\n/** @internal */\nexport const next = <S>(state: S): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Next({ state }))\n\n/** @internal */\n// oxlint-disable-next-line no-unnecessary-type-parameters -- `stop<number>()` types a loop's state where inference can't.\nexport const stop = <S = never>(): Stream.Stream<Step<never, S>> => Stream.succeed(Steps.Stop())\n\nconst isValue = <A, S>(step: Step<A, S>): step is Data.TaggedEnum.Value<Step<A, S>, \"Value\"> =>\n Predicate.isTagged(step, \"Value\")\n\n// A body that ends without `next` or `stop` stops too.\n/** @internal */\nexport const loop = <S, A, E, R>(\n initial: S,\n body: (state: S) => Stream.Stream<Step<A, S>, E, R>\n): Stream.Stream<A, E, R> => {\n const go = (state: S): Stream.Stream<A, E, R> =>\n Stream.suspend(() => {\n let after: Option.Option<S> = Option.none()\n\n return body(state).pipe(\n Stream.tap((step) =>\n Effect.sync(() => (after = Predicate.isTagged(step, \"Next\") ? Option.some(step.state) : after))\n ),\n Stream.filter(isValue),\n Stream.map((step) => step.value),\n Stream.concat(Stream.suspend(() => Option.match(after, { onNone: () => Stream.empty, onSome: go })))\n )\n })\n\n return go(initial)\n}\n\n/** @internal */\nexport const step = <R, S, E2 = never, R2 = never>(\n options: StepOptions<R>,\n then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>\n): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | R | R2> => {\n const toolkit = options.toolkit ?? Toolkits.empty\n const instructions = Predicate.isString(options.instructions) ? { text: options.instructions } : options.instructions\n\n const after = (result: StepResult) => Stream.suspend(() => then(result))\n\n return Stream.unwrap(\n Effect.gen(function* () {\n const { messages: history, throughSequence } = readHistory(yield* options.history)\n // Calls a crash cut off, with no result: they run again, with the same ids, before the model goes on.\n const cutOff = History.unansweredCalls(history)\n const approvals = yield* TurnApprovals\n\n // Calls the user approved: they run as the model made them, with the same ids, before the model goes on. Not in\n // a loop without their tools.\n const approved = Option.isNone(approvals)\n ? []\n : (yield* approvals.value.approved).filter(\n (call) =>\n toolkit.definitions.some((tool) => tool.name === call.name) && !cutOff.some((cut) => cut.id === call.id)\n )\n\n if (cutOff.length > 0 || approved.length > 0) {\n const pending = [\n ...cutOff.map((call) => ({ call, approved: false })),\n ...approved.map((call) => ({ call, approved: true }))\n ]\n\n return runCalls(toolkit, pending, history, ({ waiting }) =>\n after({ output: undefined, done: waiting, historyLength: history.length + pending.length })\n )\n }\n\n const prompt = Prompt.fromMessages([Prompt.systemMessage({ content: instructions.text }), ...history])\n const name = yield* Effect.serviceOption(Model.ModelName)\n const started = yield* Clock.currentTimeMillis\n\n const response = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(\n Telemetry.annotateModelCall(instructions.attributes)\n )\n\n const metadata = response.content.find((part) => part.type === \"response-metadata\")\n\n const output = AssistantEvent.ModelOutput.make({\n parts: modelParts(response.content),\n seenThroughSequence: throughSequence,\n model: metadata?.modelId ?? Option.getOrUndefined(name),\n responseId: metadata?.id,\n usage: response.usage,\n durationMillis: (yield* Clock.currentTimeMillis) - started\n })\n\n const calls = toolCalls(output.parts)\n const historyLength = history.length + Prompt.fromResponseParts(output.parts).content.length + calls.length\n\n // A failed call doesn't end the turn: a message that failed never reached the user, so the model reads why. A call\n // that waits for approval does: the turn waits for the user.\n return Stream.make(value(output)).pipe(\n Stream.concat(\n runCalls(\n toolkit,\n calls.map((call) => ({ call, approved: false })),\n history,\n ({ failed, waiting }) =>\n after({\n output,\n done: calls.length === 0 || waiting || (!failed && calls.some(isTurnEnding)),\n historyLength\n })\n )\n )\n )\n })\n )\n}\n\n/** @internal */\nexport const run = <R = never>(\n options: Options<R>\n): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | R> => {\n const maxSteps = options.maxSteps ?? 16\n\n // Done: stop, unless something arrived while the model was working; then it reads that first.\n const afterDone = (steps: number, historyLength: number) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const read = yield* options.history\n\n return readHistory(read).messages.length > historyLength ? next(steps) : stop<number>()\n })\n )\n\n return loop(0, (steps: number) =>\n step(options, (result) => {\n const taken = result.output === undefined ? steps : steps + 1\n\n if (taken >= maxSteps) return stop<number>()\n\n return result.done ? afterDone(taken, result.historyLength) : next(taken)\n })\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgCA,MAAa,gBAAgB,QAAQ,UAEnC,kCAAkC,EAAE,cAAc,OAAO,KAAK,CAAC;;AAKjE,MAAa,YAAY,QAAQ,UAAyC,8BAA8B,EACtG,cAAc,OAAO,KACvB,CAAC;AAQD,MAAM,eAAe,SACnB,cAAc,OACV;CAAE,UAAU,KAAK;CAAU,iBAAiB,KAAK;AAAgB,IACjE;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,QAAQ,cACT,MAAM;GACJ;GACA,MAAM,MAAM,WAAW,KAAK,WAAY,CAAC,UAAU,MAAM,KAAK,YAAY;GAC1E;EACF,CAAC,CACL,CACF,CACF;CACF,CAAC,CACH;AACF;;AAGA,MAAa,OACX,YAC2E;CAC3E,MAAM,WAAW,QAAQ,YAAY;CAGrC,MAAM,aAAa,OAAe,kBAChC,OAAO,OACL,OAAO,IAAI,aAAa;EACtB,MAAM,OAAO,OAAO,QAAQ;EAE5B,OAAO,YAAY,IAAI,CAAC,CAAC,SAAS,SAAS,gBAAgB,KAAK,KAAK,IAAI,KAAa;CACxF,CAAC,CACH;CAEF,OAAO,KAAK,IAAI,UACd,KAAK,UAAU,WAAW;EACxB,MAAM,QAAQ,OAAO,WAAW,KAAA,IAAY,QAAQ,QAAQ;EAE5D,IAAI,SAAS,UAAU,OAAO,KAAa;EAE3C,OAAO,OAAO,OAAO,UAAU,OAAO,OAAO,aAAa,IAAI,KAAK,KAAK;CAC1E,CAAC,CACH;AACF"}
|
|
@@ -32,39 +32,31 @@ const agentInput = (input) => Effect.annotateCurrentSpan("input.value", toJsonTe
|
|
|
32
32
|
const agentOutput = (output) => Effect.annotateCurrentSpan("output.value", toJsonText(output));
|
|
33
33
|
const encodePrompt = Schema.encodeOption(Prompt.Prompt);
|
|
34
34
|
const encodeParts = Schema.encodeOption(Schema.Array(ModelPart));
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
const completionOf = (response) => encodeParts(response.flatMap((part) => Option.toArray(Schema.decodeUnknownOption(ModelPart)(part))));
|
|
36
|
+
const annotateModelSpan = ({ span, prompt, response }, name, attributes) => {
|
|
37
|
+
span.attribute("gen_ai.prompt", toJsonText(Option.map(encodePrompt(prompt), (encoded) => encoded.content)));
|
|
38
|
+
span.attribute("gen_ai.completion", toJsonText(Option.getOrUndefined(completionOf(response))));
|
|
39
|
+
for (const [key, value] of Object.entries(attributes ?? {})) span.attribute(key, value);
|
|
40
|
+
const finish = response.find((part) => part.type === "finish");
|
|
38
41
|
Telemetry.addGenAIAnnotations(span, {
|
|
39
42
|
operation: { name: "chat" },
|
|
40
43
|
request: { model: Option.getOrUndefined(name) },
|
|
41
|
-
response: { finishReasons: [
|
|
44
|
+
response: { finishReasons: finish === void 0 ? void 0 : [finish.reason] },
|
|
42
45
|
usage: {
|
|
43
|
-
inputTokens: usage.inputTokens.total,
|
|
44
|
-
outputTokens: usage.outputTokens.total
|
|
46
|
+
inputTokens: finish?.usage.inputTokens.total,
|
|
47
|
+
outputTokens: finish?.usage.outputTokens.total
|
|
45
48
|
}
|
|
46
49
|
});
|
|
47
|
-
|
|
48
|
-
span.attribute("gen_ai.
|
|
49
|
-
|
|
50
|
-
if (usage.inputTokens.cacheWrite !== void 0) span.attribute("gen_ai.usage.cache_creation_input_tokens", usage.inputTokens.cacheWrite);
|
|
51
|
-
});
|
|
50
|
+
if (finish?.usage.inputTokens.cacheRead !== void 0) span.attribute("gen_ai.usage.cache_read_input_tokens", finish.usage.inputTokens.cacheRead);
|
|
51
|
+
if (finish?.usage.inputTokens.cacheWrite !== void 0) span.attribute("gen_ai.usage.cache_creation_input_tokens", finish.usage.inputTokens.cacheWrite);
|
|
52
|
+
};
|
|
52
53
|
/** @internal */
|
|
53
|
-
const
|
|
54
|
+
const annotateModelCall = (attributes) => (self) => Effect.gen(function* () {
|
|
54
55
|
const name = yield* Effect.serviceOption(Model.ModelName);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}).pipe(Effect.withSpan(Option.match(name, {
|
|
60
|
-
onNone: () => "chat",
|
|
61
|
-
onSome: (model) => `chat ${model}`
|
|
62
|
-
}), {
|
|
63
|
-
kind: "client",
|
|
64
|
-
attributes: {
|
|
65
|
-
"gen_ai.prompt": toJsonText(Option.map(encodePrompt(prompt), (encoded) => encoded.content)),
|
|
66
|
-
...attributes
|
|
67
|
-
}
|
|
56
|
+
const outer = yield* Effect.serviceOption(Telemetry.CurrentSpanTransformer);
|
|
57
|
+
return yield* self.pipe(Effect.provideService(Telemetry.CurrentSpanTransformer, (call) => {
|
|
58
|
+
if (Option.isSome(outer)) outer.value(call);
|
|
59
|
+
annotateModelSpan(call, name, attributes);
|
|
68
60
|
}));
|
|
69
61
|
});
|
|
70
62
|
/** @internal */
|
|
@@ -80,6 +72,6 @@ const toolSpan = (call) => Effect.withSpan(`execute_tool ${call.name}`, {
|
|
|
80
72
|
/** @internal */
|
|
81
73
|
const toolOutput = (result) => Effect.annotateCurrentSpan("output.value", Schema.is(Schema.String)(result) ? result : toJsonText(result));
|
|
82
74
|
//#endregion
|
|
83
|
-
export { agentInput, agentOutput, agentSpan,
|
|
75
|
+
export { agentInput, agentOutput, agentSpan, annotateModelCall, currentTraceId, toolOutput, toolSpan };
|
|
84
76
|
|
|
85
77
|
//# sourceMappingURL=telemetry.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"telemetry.js","names":["AssistantEvent.ModelPart"],"sources":["../../src/internal/telemetry.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"telemetry.js","names":["AssistantEvent.ModelPart"],"sources":["../../src/internal/telemetry.ts"],"sourcesContent":["import * as Model from \"effect/ai/Model\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport * as Telemetry from \"effect/ai/Telemetry\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as References from \"effect/References\"\nimport * as Schema from \"effect/Schema\"\nimport * as Tracer from \"effect/Tracer\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type { SpanAttributes } from \"../Instructions.ts\"\nimport { toJsonText } from \"./schema.ts\"\n\ntype Traced = string | ReadonlyArray<{ readonly _tag: string }>\n\n// Spans follow the OpenTelemetry GenAI conventions, so LLM tools (Langfuse, Phoenix, Datadog LLM Observability) can\n// pick the agent work out of a trace.\n/** @internal */\nexport const agentSpan = (name: string, options: { readonly chatId: string; readonly root?: boolean }) =>\n Effect.withSpan(`invoke_agent ${name}`, {\n kind: \"internal\",\n root: options.root ?? false,\n attributes: {\n \"gen_ai.operation.name\": \"invoke_agent\",\n \"gen_ai.agent.name\": name,\n \"gen_ai.conversation.id\": options.chatId,\n \"session.id\": options.chatId\n }\n })\n\n// None when tracing is off, or when the tracer is Effect's own, which exports nothing.\n/** @internal */\nexport const currentTraceId: Effect.Effect<Option.Option<string>> = Effect.gen(function* () {\n const tracer = yield* Tracer.Tracer\n\n if (!(yield* References.TracerEnabled) || tracer === Tracer.nativeTracer) return Option.none()\n\n return Option.map(yield* Effect.option(Effect.currentSpan), (span) => span.traceId)\n})\n\n/** @internal */\nexport const agentInput = (input: Traced) => Effect.annotateCurrentSpan(\"input.value\", toJsonText(input))\n\n/** @internal */\nexport const agentOutput = (output: Traced) => Effect.annotateCurrentSpan(\"output.value\", toJsonText(output))\n\nconst encodePrompt = Schema.encodeOption(Prompt.Prompt)\n\nconst encodeParts = Schema.encodeOption(Schema.Array(AssistantEvent.ModelPart))\n\ntype ModelCall = Parameters<Telemetry.SpanTransformer>[0]\n\nconst completionOf = (response: ModelCall[\"response\"]) =>\n encodeParts(response.flatMap((part) => Option.toArray(Schema.decodeUnknownOption(AssistantEvent.ModelPart)(part))))\n\nconst annotateModelSpan = (\n { span, prompt, response }: ModelCall,\n name: Option.Option<string>,\n attributes: SpanAttributes | undefined\n) => {\n span.attribute(\"gen_ai.prompt\", toJsonText(Option.map(encodePrompt(prompt), (encoded) => encoded.content)))\n span.attribute(\"gen_ai.completion\", toJsonText(Option.getOrUndefined(completionOf(response))))\n\n for (const [key, value] of Object.entries(attributes ?? {})) {\n span.attribute(key, value)\n }\n\n const finish = response.find((part) => part.type === \"finish\")\n\n Telemetry.addGenAIAnnotations(span, {\n operation: { name: \"chat\" },\n request: { model: Option.getOrUndefined(name) },\n response: { finishReasons: finish === undefined ? undefined : [finish.reason] },\n usage: {\n inputTokens: finish?.usage.inputTokens.total,\n outputTokens: finish?.usage.outputTokens.total\n }\n })\n\n if (finish?.usage.inputTokens.cacheRead !== undefined) {\n span.attribute(\"gen_ai.usage.cache_read_input_tokens\", finish.usage.inputTokens.cacheRead)\n }\n\n if (finish?.usage.inputTokens.cacheWrite !== undefined) {\n span.attribute(\"gen_ai.usage.cache_creation_input_tokens\", finish.usage.inputTokens.cacheWrite)\n }\n}\n\n/** @internal */\nexport const annotateModelCall =\n (attributes: SpanAttributes | undefined) =>\n <A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Effect.gen(function* () {\n const name = yield* Effect.serviceOption(Model.ModelName)\n const outer = yield* Effect.serviceOption(Telemetry.CurrentSpanTransformer)\n\n return yield* self.pipe(\n Effect.provideService(Telemetry.CurrentSpanTransformer, (call) => {\n if (Option.isSome(outer)) outer.value(call)\n\n annotateModelSpan(call, name, attributes)\n })\n )\n })\n\n/** @internal */\nexport const toolSpan = (call: AssistantEvent.ToolCallPart) =>\n Effect.withSpan(`execute_tool ${call.name}`, {\n kind: \"internal\",\n attributes: {\n \"gen_ai.operation.name\": \"execute_tool\",\n \"gen_ai.tool.name\": call.name,\n \"gen_ai.tool.call.id\": call.id,\n \"input.value\": toJsonText({ arguments: call.params })\n }\n })\n\n/** @internal */\nexport const toolOutput = (result: Schema.Json) =>\n Effect.annotateCurrentSpan(\"output.value\", Schema.is(Schema.String)(result) ? result : toJsonText(result))\n"],"mappings":";;;;;;;;;;;;AAiBA,MAAa,aAAa,MAAc,YACtC,OAAO,SAAS,gBAAgB,QAAQ;CACtC,MAAM;CACN,MAAM,QAAQ,QAAQ;CACtB,YAAY;EACV,yBAAyB;EACzB,qBAAqB;EACrB,0BAA0B,QAAQ;EAClC,cAAc,QAAQ;CACxB;AACF,CAAC;;AAIH,MAAa,iBAAuD,OAAO,IAAI,aAAa;CAC1F,MAAM,SAAS,OAAO,OAAO;CAE7B,IAAI,EAAE,OAAO,WAAW,kBAAkB,WAAW,OAAO,cAAc,OAAO,OAAO,KAAK;CAE7F,OAAO,OAAO,IAAI,OAAO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AAU,4CAA4C,OAAO,MAAM,YAAY,UAAU;AAElG;;AAGA,MAAa,qBACV,gBACS,SACR,OAAO,IAAI,aAAa;CACtB,MAAM,OAAO,OAAO,OAAO,cAAc,MAAM,SAAS;CACxD,MAAM,QAAQ,OAAO,OAAO,cAAc,UAAU,sBAAsB;CAE1E,OAAO,OAAO,KAAK,KACjB,OAAO,eAAe,UAAU,yBAAyB,SAAS;EAChE,IAAI,OAAO,OAAO,KAAK,GAAG,MAAM,MAAM,IAAI;EAE1C,kBAAkB,MAAM,MAAM,UAAU;CAC1C,CAAC,CACH;AACF,CAAC;;AAGL,MAAa,YAAY,SACvB,OAAO,SAAS,gBAAgB,KAAK,QAAQ;CAC3C,MAAM;CACN,YAAY;EACV,yBAAyB;EACzB,oBAAoB,KAAK;EACzB,uBAAuB,KAAK;EAC5B,eAAe,WAAW,EAAE,WAAW,KAAK,OAAO,CAAC;CACtD;AACF,CAAC;;AAGH,MAAa,cAAc,WACzB,OAAO,oBAAoB,gBAAgB,OAAO,GAAG,OAAO,MAAM,CAAC,CAAC,MAAM,IAAI,SAAS,WAAW,MAAM,CAAC"}
|
package/package.json
CHANGED
package/src/internal/loop.ts
CHANGED
|
@@ -311,7 +311,7 @@ export const step = <R, S, E2 = never, R2 = never>(
|
|
|
311
311
|
const started = yield* Clock.currentTimeMillis
|
|
312
312
|
|
|
313
313
|
const response = yield* generate(prompt, toolkit.definitions, options.toolChoice).pipe(
|
|
314
|
-
Telemetry.
|
|
314
|
+
Telemetry.annotateModelCall(instructions.attributes)
|
|
315
315
|
)
|
|
316
316
|
|
|
317
317
|
const metadata = response.content.find((part) => part.type === "response-metadata")
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import type * as LanguageModel from "effect/ai/LanguageModel"
|
|
2
1
|
import * as Model from "effect/ai/Model"
|
|
3
2
|
import * as Prompt from "effect/ai/Prompt"
|
|
4
3
|
import * as Telemetry from "effect/ai/Telemetry"
|
|
5
|
-
import type * as Tool from "effect/ai/Tool"
|
|
6
4
|
import * as Effect from "effect/Effect"
|
|
7
5
|
import * as Option from "effect/Option"
|
|
8
6
|
import * as References from "effect/References"
|
|
@@ -49,61 +47,57 @@ const encodePrompt = Schema.encodeOption(Prompt.Prompt)
|
|
|
49
47
|
|
|
50
48
|
const encodeParts = Schema.encodeOption(Schema.Array(AssistantEvent.ModelPart))
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
type ModelCall = Parameters<Telemetry.SpanTransformer>[0]
|
|
51
|
+
|
|
52
|
+
const completionOf = (response: ModelCall["response"]) =>
|
|
53
|
+
encodeParts(response.flatMap((part) => Option.toArray(Schema.decodeUnknownOption(AssistantEvent.ModelPart)(part))))
|
|
54
|
+
|
|
55
|
+
const annotateModelSpan = (
|
|
56
|
+
{ span, prompt, response }: ModelCall,
|
|
57
|
+
name: Option.Option<string>,
|
|
58
|
+
attributes: SpanAttributes | undefined
|
|
59
|
+
) => {
|
|
60
|
+
span.attribute("gen_ai.prompt", toJsonText(Option.map(encodePrompt(prompt), (encoded) => encoded.content)))
|
|
61
|
+
span.attribute("gen_ai.completion", toJsonText(Option.getOrUndefined(completionOf(response))))
|
|
62
|
+
|
|
63
|
+
for (const [key, value] of Object.entries(attributes ?? {})) {
|
|
64
|
+
span.attribute(key, value)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const finish = response.find((part) => part.type === "finish")
|
|
58
68
|
|
|
59
69
|
Telemetry.addGenAIAnnotations(span, {
|
|
60
70
|
operation: { name: "chat" },
|
|
61
71
|
request: { model: Option.getOrUndefined(name) },
|
|
62
|
-
response: { finishReasons: [
|
|
72
|
+
response: { finishReasons: finish === undefined ? undefined : [finish.reason] },
|
|
63
73
|
usage: {
|
|
64
|
-
inputTokens: usage.inputTokens.total,
|
|
65
|
-
outputTokens: usage.outputTokens.total
|
|
74
|
+
inputTokens: finish?.usage.inputTokens.total,
|
|
75
|
+
outputTokens: finish?.usage.outputTokens.total
|
|
66
76
|
}
|
|
67
77
|
})
|
|
68
78
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
span.attribute("gen_ai.completion", toJsonText(Option.getOrUndefined(completion)))
|
|
74
|
-
|
|
75
|
-
if (usage.inputTokens.cacheRead !== undefined) {
|
|
76
|
-
span.attribute("gen_ai.usage.cache_read_input_tokens", usage.inputTokens.cacheRead)
|
|
79
|
+
if (finish?.usage.inputTokens.cacheRead !== undefined) {
|
|
80
|
+
span.attribute("gen_ai.usage.cache_read_input_tokens", finish.usage.inputTokens.cacheRead)
|
|
77
81
|
}
|
|
78
82
|
|
|
79
|
-
if (usage.inputTokens.cacheWrite !== undefined) {
|
|
80
|
-
span.attribute("gen_ai.usage.cache_creation_input_tokens", usage.inputTokens.cacheWrite)
|
|
83
|
+
if (finish?.usage.inputTokens.cacheWrite !== undefined) {
|
|
84
|
+
span.attribute("gen_ai.usage.cache_creation_input_tokens", finish.usage.inputTokens.cacheWrite)
|
|
81
85
|
}
|
|
82
|
-
}
|
|
86
|
+
}
|
|
83
87
|
|
|
84
|
-
// Named after the model when the context has its name (`AnthropicLanguageModel.model(...)` provides it).
|
|
85
88
|
/** @internal */
|
|
86
|
-
export const
|
|
87
|
-
(
|
|
88
|
-
<
|
|
89
|
-
self: Effect.Effect<LanguageModel.GenerateTextResponse<Tools, "opaque">, E, R>
|
|
90
|
-
): Effect.Effect<LanguageModel.GenerateTextResponse<Tools, "opaque">, E, R> =>
|
|
89
|
+
export const annotateModelCall =
|
|
90
|
+
(attributes: SpanAttributes | undefined) =>
|
|
91
|
+
<A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
|
|
91
92
|
Effect.gen(function* () {
|
|
92
93
|
const name = yield* Effect.serviceOption(Model.ModelName)
|
|
94
|
+
const outer = yield* Effect.serviceOption(Telemetry.CurrentSpanTransformer)
|
|
93
95
|
|
|
94
|
-
return yield*
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
yield* annotateChat(response, name).pipe(Effect.ignore)
|
|
96
|
+
return yield* self.pipe(
|
|
97
|
+
Effect.provideService(Telemetry.CurrentSpanTransformer, (call) => {
|
|
98
|
+
if (Option.isSome(outer)) outer.value(call)
|
|
98
99
|
|
|
99
|
-
|
|
100
|
-
}).pipe(
|
|
101
|
-
Effect.withSpan(Option.match(name, { onNone: () => "chat", onSome: (model) => `chat ${model}` }), {
|
|
102
|
-
kind: "client",
|
|
103
|
-
attributes: {
|
|
104
|
-
"gen_ai.prompt": toJsonText(Option.map(encodePrompt(prompt), (encoded) => encoded.content)),
|
|
105
|
-
...attributes
|
|
106
|
-
}
|
|
100
|
+
annotateModelSpan(call, name, attributes)
|
|
107
101
|
})
|
|
108
102
|
)
|
|
109
103
|
})
|