taugraph 0.2.0 → 0.3.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/CHANGELOG.md +62 -0
- package/README.md +232 -18
- package/README.zh-CN.md +232 -18
- package/dist/agent/index.d.ts +66 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +228 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/state.d.ts +30 -0
- package/dist/agent/state.d.ts.map +1 -0
- package/dist/agent/state.js +135 -0
- package/dist/agent/state.js.map +1 -0
- package/dist/agent/util.d.ts +9 -0
- package/dist/agent/util.d.ts.map +1 -0
- package/dist/agent/util.js +28 -0
- package/dist/agent/util.js.map +1 -0
- package/dist/nodes/content.d.ts +18 -0
- package/dist/nodes/content.d.ts.map +1 -0
- package/dist/nodes/content.js +88 -0
- package/dist/nodes/content.js.map +1 -0
- package/dist/nodes/index.d.ts +2 -1
- package/dist/nodes/index.d.ts.map +1 -1
- package/dist/nodes/index.js +1 -0
- package/dist/nodes/index.js.map +1 -1
- package/dist/nodes/llm.d.ts +98 -4
- package/dist/nodes/llm.d.ts.map +1 -1
- package/dist/nodes/llm.js.map +1 -1
- package/dist/openai/index.d.ts +39 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +178 -0
- package/dist/openai/index.js.map +1 -0
- package/package.json +15 -3
- package/src/agent/index.ts +315 -0
- package/src/agent/state.ts +147 -0
- package/src/agent/util.ts +35 -0
- package/src/nodes/content.ts +100 -0
- package/src/nodes/index.ts +10 -0
- package/src/nodes/llm.ts +102 -6
- package/src/openai/index.ts +246 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// The agent's state and its schema, written against Standard Schema by hand so the agent needs no validator.
|
|
2
|
+
import type { StandardSchemaV1 } from "../index.js";
|
|
3
|
+
import type { ChatMessage, ToolCall } from "../nodes/index.js";
|
|
4
|
+
|
|
5
|
+
export type AgentStop = "done" | "max_rounds";
|
|
6
|
+
|
|
7
|
+
export interface AgentState {
|
|
8
|
+
/** The conversation without the system prompt, which comes from the config on every call. */
|
|
9
|
+
readonly messages: readonly ChatMessage[];
|
|
10
|
+
/** Calls from the last assistant message that have not run yet, in order. */
|
|
11
|
+
readonly pending: readonly ToolCall[];
|
|
12
|
+
/** Model calls made in this run. */
|
|
13
|
+
readonly rounds: number;
|
|
14
|
+
/** The last answer, once the model stops calling tools. */
|
|
15
|
+
readonly final: string | null;
|
|
16
|
+
readonly stop: AgentStop | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** What a run starts from: a conversation, and optionally where an earlier run left off. */
|
|
20
|
+
export interface AgentInput {
|
|
21
|
+
readonly messages: readonly ChatMessage[];
|
|
22
|
+
readonly pending?: readonly ToolCall[];
|
|
23
|
+
readonly rounds?: number;
|
|
24
|
+
readonly final?: string | null;
|
|
25
|
+
readonly stop?: AgentStop | null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type Issue = StandardSchemaV1.Issue;
|
|
29
|
+
type Path = (string | number)[];
|
|
30
|
+
|
|
31
|
+
const isObject = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
32
|
+
|
|
33
|
+
function checkCall(value: unknown, path: Path, issues: Issue[]): void {
|
|
34
|
+
if (!isObject(value)) {
|
|
35
|
+
issues.push({ message: "expected a tool call object", path });
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (typeof value.id !== "string") issues.push({ message: "expected a string", path: [...path, "id"] });
|
|
39
|
+
if (typeof value.name !== "string") issues.push({ message: "expected a string", path: [...path, "name"] });
|
|
40
|
+
if (!isObject(value.arguments)) issues.push({ message: "expected an object", path: [...path, "arguments"] });
|
|
41
|
+
if (value.parseError !== undefined && typeof value.parseError !== "string")
|
|
42
|
+
issues.push({ message: "expected a string", path: [...path, "parseError"] });
|
|
43
|
+
if (value.signature !== undefined && typeof value.signature !== "string")
|
|
44
|
+
issues.push({ message: "expected a string", path: [...path, "signature"] });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function checkPart(value: unknown, path: Path, issues: Issue[]): void {
|
|
48
|
+
if (!isObject(value)) {
|
|
49
|
+
issues.push({ message: "expected a content part object", path });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (value.type === "text") {
|
|
53
|
+
if (typeof value.text !== "string") issues.push({ message: "expected a string", path: [...path, "text"] });
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (value.type !== "image") {
|
|
57
|
+
issues.push({ message: 'expected "text" or "image"', path: [...path, "type"] });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (value.data !== undefined) {
|
|
61
|
+
if (typeof value.data !== "string") issues.push({ message: "expected a string", path: [...path, "data"] });
|
|
62
|
+
if (typeof value.mimeType !== "string") issues.push({ message: "expected a string", path: [...path, "mimeType"] });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (typeof value.url !== "string")
|
|
66
|
+
issues.push({ message: "expected a string (or data and mimeType)", path: [...path, "url"] });
|
|
67
|
+
if (value.mimeType !== undefined && typeof value.mimeType !== "string")
|
|
68
|
+
issues.push({ message: "expected a string", path: [...path, "mimeType"] });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Checks a message's `content`: a string, or, where `parts` is true, also an array of text and image parts. */
|
|
72
|
+
export function checkContent(value: unknown, path: Path, issues: Issue[], parts: boolean): void {
|
|
73
|
+
if (typeof value === "string") return;
|
|
74
|
+
if (!parts || !Array.isArray(value)) {
|
|
75
|
+
issues.push({ message: parts ? "expected a string or an array of content parts" : "expected a string", path });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
for (const [i, part] of value.entries()) checkPart(part, [...path, i], issues);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function checkReasoning(value: unknown, path: Path, issues: Issue[]): void {
|
|
82
|
+
if (!isObject(value)) {
|
|
83
|
+
issues.push({ message: "expected a reasoning part object", path });
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (typeof value.text !== "string") issues.push({ message: "expected a string", path: [...path, "text"] });
|
|
87
|
+
if (value.signature !== undefined && typeof value.signature !== "string")
|
|
88
|
+
issues.push({ message: "expected a string", path: [...path, "signature"] });
|
|
89
|
+
if (value.redacted !== undefined && typeof value.redacted !== "boolean")
|
|
90
|
+
issues.push({ message: "expected a boolean", path: [...path, "redacted"] });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function checkMessage(value: unknown, path: Path, issues: Issue[]): void {
|
|
94
|
+
if (!isObject(value)) {
|
|
95
|
+
issues.push({ message: "expected a message object", path });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const { role } = value;
|
|
99
|
+
if (role !== "system" && role !== "user" && role !== "assistant" && role !== "tool") {
|
|
100
|
+
issues.push({ message: 'expected "system", "user", "assistant" or "tool"', path: [...path, "role"] });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
checkContent(value.content, [...path, "content"], issues, role === "user" || role === "tool");
|
|
104
|
+
if (role === "assistant" && value.toolCalls !== undefined) {
|
|
105
|
+
if (!Array.isArray(value.toolCalls)) issues.push({ message: "expected an array", path: [...path, "toolCalls"] });
|
|
106
|
+
else for (const [i, c] of value.toolCalls.entries()) checkCall(c, [...path, "toolCalls", i], issues);
|
|
107
|
+
}
|
|
108
|
+
if (role === "assistant") {
|
|
109
|
+
if (value.model !== undefined && typeof value.model !== "string")
|
|
110
|
+
issues.push({ message: "expected a string", path: [...path, "model"] });
|
|
111
|
+
if (value.reasoning !== undefined) {
|
|
112
|
+
if (!Array.isArray(value.reasoning)) issues.push({ message: "expected an array", path: [...path, "reasoning"] });
|
|
113
|
+
else for (const [i, r] of value.reasoning.entries()) checkReasoning(r, [...path, "reasoning", i], issues);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (role === "tool") {
|
|
117
|
+
if (typeof value.toolCallId !== "string")
|
|
118
|
+
issues.push({ message: "expected a string", path: [...path, "toolCallId"] });
|
|
119
|
+
if (value.isError !== undefined && typeof value.isError !== "boolean")
|
|
120
|
+
issues.push({ message: "expected a boolean", path: [...path, "isError"] });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Validates an agent state, filling in `pending: []`, `rounds: 0`, `final: null` and `stop: null`. */
|
|
125
|
+
export const agentStateSchema: StandardSchemaV1<AgentInput, AgentState> = {
|
|
126
|
+
"~standard": {
|
|
127
|
+
version: 1,
|
|
128
|
+
vendor: "taugraph",
|
|
129
|
+
validate(value) {
|
|
130
|
+
if (!isObject(value)) return { issues: [{ message: "expected an object" }] };
|
|
131
|
+
const issues: Issue[] = [];
|
|
132
|
+
const { messages, pending = [], rounds = 0, final = null, stop = null } = value;
|
|
133
|
+
if (!Array.isArray(messages)) issues.push({ message: "expected an array", path: ["messages"] });
|
|
134
|
+
else for (const [i, m] of messages.entries()) checkMessage(m, ["messages", i], issues);
|
|
135
|
+
if (!Array.isArray(pending)) issues.push({ message: "expected an array", path: ["pending"] });
|
|
136
|
+
else for (const [i, c] of pending.entries()) checkCall(c, ["pending", i], issues);
|
|
137
|
+
if (!Number.isInteger(rounds) || (rounds as number) < 0)
|
|
138
|
+
issues.push({ message: "expected a non-negative integer", path: ["rounds"] });
|
|
139
|
+
if (final !== null && typeof final !== "string")
|
|
140
|
+
issues.push({ message: "expected a string or null", path: ["final"] });
|
|
141
|
+
if (stop !== null && stop !== "done" && stop !== "max_rounds")
|
|
142
|
+
issues.push({ message: 'expected "done", "max_rounds" or null', path: ["stop"] });
|
|
143
|
+
if (issues.length > 0) return { issues };
|
|
144
|
+
return { value: { messages, pending, rounds, final, stop } as AgentState };
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { MaybePromise, StandardSchemaV1 } from "../index.js";
|
|
2
|
+
|
|
3
|
+
export function isThenable<T>(value: MaybePromise<T>): value is PromiseLike<T> {
|
|
4
|
+
return value !== null && typeof value === "object" && typeof (value as PromiseLike<T>).then === "function";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** Chains without introducing a Promise when the value is synchronous, so the agent stays `runSync`-friendly. */
|
|
8
|
+
export function andThen<A, B>(value: MaybePromise<A>, fn: (value: A) => MaybePromise<B>): MaybePromise<B> {
|
|
9
|
+
return isThenable(value) ? Promise.resolve(value).then(fn) : fn(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Runs `run` and hands its value to `ok`, or its error (thrown or rejected) to `fail`. Sync in, sync out. */
|
|
13
|
+
export function settle<T, R>(
|
|
14
|
+
run: () => MaybePromise<T>,
|
|
15
|
+
ok: (value: T) => MaybePromise<R>,
|
|
16
|
+
fail: (error: unknown) => MaybePromise<R>,
|
|
17
|
+
): MaybePromise<R> {
|
|
18
|
+
let value: MaybePromise<T>;
|
|
19
|
+
try {
|
|
20
|
+
value = run();
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return fail(error);
|
|
23
|
+
}
|
|
24
|
+
return isThenable(value) ? Promise.resolve(value).then(ok, fail) : ok(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Schema issues as one line the model can act on: "city: expected string; days: too small". */
|
|
28
|
+
export function issuesText(issues: readonly StandardSchemaV1.Issue[]): string {
|
|
29
|
+
return issues
|
|
30
|
+
.map((issue) => {
|
|
31
|
+
const path = (issue.path ?? []).map((p) => String(typeof p === "object" ? p.key : p)).join(".");
|
|
32
|
+
return path ? `${path}: ${issue.message}` : issue.message;
|
|
33
|
+
})
|
|
34
|
+
.join("; ");
|
|
35
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Helpers for adapters: the text of a message, and images given by URL turned into what they stand for.
|
|
2
|
+
import type { MaybePromise } from "../index.js";
|
|
3
|
+
import type { ChatMessage, ContentPart, ImagePart, MessageContent } from "./llm.js";
|
|
4
|
+
import { andThen } from "./util.js";
|
|
5
|
+
|
|
6
|
+
export interface TextOfOptions {
|
|
7
|
+
/** Written in place of each image. Without it, an image is an error. */
|
|
8
|
+
readonly imagePlaceholder?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The text of a message's content, for adapters that send text only. Images are an error unless replaced. */
|
|
12
|
+
export function textOf(content: MessageContent, options: TextOfOptions = {}): string {
|
|
13
|
+
if (typeof content === "string") return content;
|
|
14
|
+
let text = "";
|
|
15
|
+
for (const part of content) {
|
|
16
|
+
if (part.type === "text") text += part.text;
|
|
17
|
+
else if (options.imagePlaceholder !== undefined) text += options.imagePlaceholder;
|
|
18
|
+
else {
|
|
19
|
+
throw new TypeError(
|
|
20
|
+
"textOf: the content has images and this adapter sends text only; pass { imagePlaceholder } to replace them",
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return text;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Reads an image URL an adapter cannot send as it is, e.g. `media:abc` from your own store. */
|
|
28
|
+
export type ImageResolver = (url: string) => MaybePromise<ImagePart>;
|
|
29
|
+
|
|
30
|
+
const DATA_URL = /^data:([\w.+-]+\/[\w.+-]+);base64,([A-Za-z0-9+/]*={0,2})$/i;
|
|
31
|
+
|
|
32
|
+
const shown = (url: string): string => (url.length > 100 ? `${url.slice(0, 100)}…` : url);
|
|
33
|
+
|
|
34
|
+
const isURLImage = (part: ContentPart): boolean => part.type === "image" && !("data" in part);
|
|
35
|
+
|
|
36
|
+
function isImagePart(value: unknown): value is ImagePart {
|
|
37
|
+
if (typeof value !== "object" || value === null) return false;
|
|
38
|
+
const v = value as Record<string, unknown>;
|
|
39
|
+
if (v.type !== "image") return false;
|
|
40
|
+
if (typeof v.data === "string") return typeof v.mimeType === "string";
|
|
41
|
+
return typeof v.url === "string" && (v.mimeType === undefined || typeof v.mimeType === "string");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function fromDataURL(url: string): ImagePart {
|
|
45
|
+
const match = DATA_URL.exec(url);
|
|
46
|
+
if (!match) throw new TypeError(`resolveImages: only base64 data: URLs are supported, got "${shown(url)}"`);
|
|
47
|
+
return { type: "image", mimeType: match[1] as string, data: match[2] as string };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolveParts(parts: readonly ContentPart[], resolve: ImageResolver | undefined): MaybePromise<ContentPart[]> {
|
|
51
|
+
const out: ContentPart[] = [];
|
|
52
|
+
const from = (start: number): MaybePromise<ContentPart[]> => {
|
|
53
|
+
for (let i = start; i < parts.length; i++) {
|
|
54
|
+
const part = parts[i] as ContentPart;
|
|
55
|
+
if (part.type === "text" || "data" in part) {
|
|
56
|
+
out.push(part);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const { url } = part;
|
|
60
|
+
if (/^data:/i.test(url)) {
|
|
61
|
+
out.push(fromDataURL(url));
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!resolve) throw new TypeError(`resolveImages: no resolver for image URL "${shown(url)}"`);
|
|
65
|
+
return andThen(resolve(url), (image) => {
|
|
66
|
+
if (!isImagePart(image))
|
|
67
|
+
throw new TypeError(`resolveImages: the resolver did not return an image for "${shown(url)}"`);
|
|
68
|
+
out.push(!("data" in image) && /^data:/i.test(image.url) ? fromDataURL(image.url) : image);
|
|
69
|
+
return from(i + 1);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
};
|
|
74
|
+
return from(0);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Replaces each image given by URL in user and tool messages: every `data:` URL, including one `resolve` returns,
|
|
79
|
+
* becomes a base64 image; other URLs become what `resolve` returns (which may be another URL). URLs are read one after
|
|
80
|
+
* another, and the result is synchronous when `resolve` is. Messages without such images are returned as the same
|
|
81
|
+
* objects.
|
|
82
|
+
*/
|
|
83
|
+
export function resolveImages(messages: readonly ChatMessage[], resolve?: ImageResolver): MaybePromise<ChatMessage[]> {
|
|
84
|
+
const out: ChatMessage[] = [];
|
|
85
|
+
const from = (start: number): MaybePromise<ChatMessage[]> => {
|
|
86
|
+
for (let i = start; i < messages.length; i++) {
|
|
87
|
+
const m = messages[i] as ChatMessage;
|
|
88
|
+
if ((m.role !== "user" && m.role !== "tool") || typeof m.content === "string" || !m.content.some(isURLImage)) {
|
|
89
|
+
out.push(m);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
return andThen(resolveParts(m.content, resolve), (content) => {
|
|
93
|
+
out.push({ ...m, content });
|
|
94
|
+
return from(i + 1);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
};
|
|
99
|
+
return from(0);
|
|
100
|
+
}
|
package/src/nodes/index.ts
CHANGED
|
@@ -2,9 +2,12 @@
|
|
|
2
2
|
// `defineNode` + `ctx.trace.append` API, the same way third-party nodes are.
|
|
3
3
|
|
|
4
4
|
export { CodeNode, type CodeNodeOptions } from "./code.js";
|
|
5
|
+
export { type ImageResolver, resolveImages, type TextOfOptions, textOf } from "./content.js";
|
|
5
6
|
export { HumanNode, type HumanNodeConfig } from "./human.js";
|
|
6
7
|
export {
|
|
7
8
|
type ChatMessage,
|
|
9
|
+
type ContentPart,
|
|
10
|
+
type ImagePart,
|
|
8
11
|
type LLMModel,
|
|
9
12
|
LLMNode,
|
|
10
13
|
LLMOutputError,
|
|
@@ -12,4 +15,11 @@ export {
|
|
|
12
15
|
type LLMResponse,
|
|
13
16
|
type LLMStructuredNodeConfig,
|
|
14
17
|
type LLMTextNodeConfig,
|
|
18
|
+
type MessageContent,
|
|
19
|
+
type ReasoningLevel,
|
|
20
|
+
type ReasoningPart,
|
|
21
|
+
type TextPart,
|
|
22
|
+
type ToolCall,
|
|
23
|
+
type ToolChoice,
|
|
24
|
+
type ToolSpec,
|
|
15
25
|
} from "./llm.js";
|
package/src/nodes/llm.ts
CHANGED
|
@@ -2,14 +2,92 @@ import { defineNode, type MaybePromise, type Node, type RetryPolicy, type Standa
|
|
|
2
2
|
import { andThen, issuesMessage } from "./util.js";
|
|
3
3
|
|
|
4
4
|
// ---------------------------------------------------------------------------
|
|
5
|
-
// Model contract.
|
|
5
|
+
// Model contract. Adapt any SDK or HTTP API to this; taugraph/openai covers OpenAI-compatible endpoints.
|
|
6
6
|
// ---------------------------------------------------------------------------
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
/** A tool the model may call. Field names follow MCP; `inputSchema` is JSON Schema for the arguments object. */
|
|
9
|
+
export interface ToolSpec {
|
|
10
|
+
/** Letters, digits, `_` and `-`, at most 64 characters: what every major provider accepts. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly description?: string;
|
|
13
|
+
readonly inputSchema: Readonly<Record<string, unknown>>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A call the model asked for. Arguments are parsed JSON, as in MCP and Anthropic; adapters convert OpenAI's string. */
|
|
17
|
+
export interface ToolCall {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly arguments: Readonly<Record<string, unknown>>;
|
|
21
|
+
/** Set by an adapter that could not read the arguments as a JSON object; `arguments` is then `{}`. */
|
|
22
|
+
readonly parseError?: string;
|
|
23
|
+
/** The provider's opaque proof for this call (Gemini's thought signature). Send it back to the same model only. */
|
|
24
|
+
readonly signature?: string;
|
|
11
25
|
}
|
|
12
26
|
|
|
27
|
+
/** How the model may use tools: freely, not at all, at least one, or this one. */
|
|
28
|
+
export type ToolChoice = "auto" | "none" | "required" | { readonly name: string };
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* How much a model should think before it answers. Adapters map it to the provider's setting; an adapter that knows
|
|
32
|
+
* which levels a model has rounds to the nearest one.
|
|
33
|
+
*/
|
|
34
|
+
export type ReasoningLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* What a model thought before answering, in the order it gave. `signature` is the provider's opaque proof and is only
|
|
38
|
+
* valid for the model that wrote it: adapters send it back when the assistant message's `model` is their own `id`.
|
|
39
|
+
*/
|
|
40
|
+
export interface ReasoningPart {
|
|
41
|
+
readonly text: string;
|
|
42
|
+
readonly signature?: string;
|
|
43
|
+
/** The provider hid the text (a safety filter); `signature` holds the encrypted payload. */
|
|
44
|
+
readonly redacted?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Text in a message. */
|
|
48
|
+
export interface TextPart {
|
|
49
|
+
readonly type: "text";
|
|
50
|
+
readonly text: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* An image in a message: its bytes in base64 (`data`, without a `data:` prefix), or a URL. A URL may be `https:`,
|
|
55
|
+
* `data:`, or a reference of your own such as `media:abc` that the adapter reads when it calls the model, which keeps
|
|
56
|
+
* traces and snapshots small. Field names follow MCP. If both `data` and `url` are present, `data` wins; narrow with
|
|
57
|
+
* `"data" in image`.
|
|
58
|
+
*/
|
|
59
|
+
export type ImagePart =
|
|
60
|
+
| { readonly type: "image"; readonly data: string; readonly mimeType: string }
|
|
61
|
+
| { readonly type: "image"; readonly url: string; readonly mimeType?: string };
|
|
62
|
+
|
|
63
|
+
export type ContentPart = TextPart | ImagePart;
|
|
64
|
+
|
|
65
|
+
/** What a user message or a tool result carries: plain text, or text and images in order. */
|
|
66
|
+
export type MessageContent = string | readonly ContentPart[];
|
|
67
|
+
|
|
68
|
+
/** One message of a conversation. User messages and tool results may carry images; the others are text. */
|
|
69
|
+
export type ChatMessage =
|
|
70
|
+
| { readonly role: "system"; readonly content: string }
|
|
71
|
+
| { readonly role: "user"; readonly content: MessageContent }
|
|
72
|
+
| {
|
|
73
|
+
readonly role: "assistant";
|
|
74
|
+
readonly content: string;
|
|
75
|
+
readonly toolCalls?: readonly ToolCall[];
|
|
76
|
+
/** What the model thought first. */
|
|
77
|
+
readonly reasoning?: readonly ReasoningPart[];
|
|
78
|
+
/**
|
|
79
|
+
* The `LLMModel.id` that wrote this message (not `LLMResponse.model`). Adapters send reasoning signatures,
|
|
80
|
+
* `redacted` parts and `ToolCall.signature` back only when it equals their own `id`.
|
|
81
|
+
*/
|
|
82
|
+
readonly model?: string;
|
|
83
|
+
}
|
|
84
|
+
| {
|
|
85
|
+
readonly role: "tool";
|
|
86
|
+
readonly toolCallId: string;
|
|
87
|
+
readonly content: MessageContent;
|
|
88
|
+
readonly isError?: boolean;
|
|
89
|
+
};
|
|
90
|
+
|
|
13
91
|
/** Everything in a request is recorded in the trace, so keep it plain JSON. */
|
|
14
92
|
export interface LLMRequest {
|
|
15
93
|
readonly messages: readonly ChatMessage[];
|
|
@@ -19,17 +97,35 @@ export interface LLMRequest {
|
|
|
19
97
|
readonly responseFormat?: "text" | "json";
|
|
20
98
|
/** Provider-specific options, passed through by the adapter. */
|
|
21
99
|
readonly extra?: Readonly<Record<string, unknown>>;
|
|
100
|
+
/** Tools the model may call; adapters translate them to the provider's format. */
|
|
101
|
+
readonly tools?: readonly ToolSpec[];
|
|
102
|
+
readonly toolChoice?: ToolChoice;
|
|
103
|
+
/** How much the model should think first. Unset: the adapter sends nothing and the model uses its default. */
|
|
104
|
+
readonly reasoning?: ReasoningLevel;
|
|
22
105
|
}
|
|
23
106
|
|
|
24
107
|
export interface LLMResponse {
|
|
25
108
|
readonly content: string;
|
|
109
|
+
/** The provider's name for the model that answered, e.g. "gpt-4o-mini-2024-07-18". Not the `LLMModel.id`. */
|
|
26
110
|
readonly model?: string;
|
|
27
111
|
readonly finishReason?: string;
|
|
28
|
-
readonly usage?: {
|
|
112
|
+
readonly usage?: {
|
|
113
|
+
readonly inputTokens?: number;
|
|
114
|
+
readonly outputTokens?: number;
|
|
115
|
+
/** Tokens spent thinking; already counted in `outputTokens`. */
|
|
116
|
+
readonly reasoningTokens?: number;
|
|
117
|
+
};
|
|
118
|
+
/** Tool calls the model asked for, in order. */
|
|
119
|
+
readonly toolCalls?: readonly ToolCall[];
|
|
120
|
+
/** What the model thought before answering, in order. */
|
|
121
|
+
readonly reasoning?: readonly ReasoningPart[];
|
|
29
122
|
}
|
|
30
123
|
|
|
31
124
|
export interface LLMModel {
|
|
32
|
-
/**
|
|
125
|
+
/**
|
|
126
|
+
* Recorded in every `llm_call` event, e.g. "openai:gpt-4o-mini" or "ollama:qwen3". It also decides which assistant
|
|
127
|
+
* messages count as this model's own for reasoning passback, so keep it stable and different for each model.
|
|
128
|
+
*/
|
|
33
129
|
readonly id: string;
|
|
34
130
|
complete(request: LLMRequest, options: { readonly signal: AbortSignal }): MaybePromise<LLMResponse>;
|
|
35
131
|
}
|