@atoms-agent/core 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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +65 -0
  3. package/dist/agent.d.ts +58 -0
  4. package/dist/agent.d.ts.map +1 -0
  5. package/dist/agent.js +118 -0
  6. package/dist/agent.js.map +1 -0
  7. package/dist/context.d.ts +33 -0
  8. package/dist/context.d.ts.map +1 -0
  9. package/dist/context.js +42 -0
  10. package/dist/context.js.map +1 -0
  11. package/dist/errors.d.ts +7 -0
  12. package/dist/errors.d.ts.map +1 -0
  13. package/dist/errors.js +6 -0
  14. package/dist/errors.js.map +1 -0
  15. package/dist/events.d.ts +68 -0
  16. package/dist/events.d.ts.map +1 -0
  17. package/dist/events.js +2 -0
  18. package/dist/events.js.map +1 -0
  19. package/dist/index.d.ts +17 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +7 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/internal/abort.d.ts +2 -0
  24. package/dist/internal/abort.d.ts.map +1 -0
  25. package/dist/internal/abort.js +34 -0
  26. package/dist/internal/abort.js.map +1 -0
  27. package/dist/internal/loop.d.ts +45 -0
  28. package/dist/internal/loop.d.ts.map +1 -0
  29. package/dist/internal/loop.js +509 -0
  30. package/dist/internal/loop.js.map +1 -0
  31. package/dist/limits.d.ts +16 -0
  32. package/dist/limits.d.ts.map +1 -0
  33. package/dist/limits.js +7 -0
  34. package/dist/limits.js.map +1 -0
  35. package/dist/registry.d.ts +20 -0
  36. package/dist/registry.d.ts.map +1 -0
  37. package/dist/registry.js +52 -0
  38. package/dist/registry.js.map +1 -0
  39. package/dist/spec.d.ts +29 -0
  40. package/dist/spec.d.ts.map +1 -0
  41. package/dist/spec.js +141 -0
  42. package/dist/spec.js.map +1 -0
  43. package/dist/state.d.ts +42 -0
  44. package/dist/state.d.ts.map +1 -0
  45. package/dist/state.js +263 -0
  46. package/dist/state.js.map +1 -0
  47. package/dist/structured-output.d.ts +41 -0
  48. package/dist/structured-output.d.ts.map +1 -0
  49. package/dist/structured-output.js +170 -0
  50. package/dist/structured-output.js.map +1 -0
  51. package/dist/tool.d.ts +75 -0
  52. package/dist/tool.d.ts.map +1 -0
  53. package/dist/tool.js +206 -0
  54. package/dist/tool.js.map +1 -0
  55. package/package.json +50 -0
package/dist/spec.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ import type { JsonValue } from "@atoms-agent/llm";
2
+ import type { RunLimits } from "./limits.js";
3
+ import { type StructuredOutputConfig } from "./structured-output.js";
4
+ /**
5
+ * AgentSpec —— 可序列化的 Agent 声明(PRD §8.1)。
6
+ *
7
+ * 只包含 Ref 与 JSON 数据;禁止 Function、Class Instance、Provider Client、
8
+ * Secret、AbortSignal(核心不变量 7)。Ref 由 RuntimeRegistry 在运行时解析。
9
+ */
10
+ export type AgentSpec = {
11
+ id: string;
12
+ model: {
13
+ ref: string;
14
+ };
15
+ systemPrompt: string;
16
+ tools: Array<{
17
+ ref: string;
18
+ config?: JsonValue;
19
+ }>;
20
+ context?: {
21
+ ref: string;
22
+ config?: JsonValue;
23
+ };
24
+ limits?: RunLimits;
25
+ structuredOutput?: StructuredOutputConfig;
26
+ };
27
+ /** Run 前的运行时边界校验;拒绝 TypeScript 类型系统外传入的非法对象。 */
28
+ export declare function assertAgentSpec(value: unknown): asserts value is AgentSpec;
29
+ //# sourceMappingURL=spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spec.d.ts","sourceRoot":"","sources":["../src/spec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAGlD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,EAAgC,KAAK,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAEnG;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE;QAAE,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IACvB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,SAAS,CAAA;KAAE,CAAC,CAAC;IAClD,OAAO,CAAC,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,SAAS,CAAA;KAAE,CAAC;IAC9C,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,gBAAgB,CAAC,EAAE,sBAAsB,CAAC;CAC3C,CAAC;AAwBF,gDAAgD;AAChD,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,IAAI,SAAS,CAwD1E"}
package/dist/spec.js ADDED
@@ -0,0 +1,141 @@
1
+ import { AtomsAgentError } from "@atoms-agent/llm";
2
+ import { MAX_TIMEOUT_MS } from "./limits.js";
3
+ import { assertStructuredOutputConfig } from "./structured-output.js";
4
+ const SPEC_KEYS = new Set([
5
+ "id",
6
+ "model",
7
+ "systemPrompt",
8
+ "tools",
9
+ "context",
10
+ "limits",
11
+ "structuredOutput",
12
+ ]);
13
+ const BINDING_KEYS = new Set(["ref", "config"]);
14
+ const MODEL_KEYS = new Set(["ref"]);
15
+ const LIMIT_KEYS = new Set(["maxTurns", "timeoutMs"]);
16
+ const SECRET_KEYS = new Set([
17
+ "apikey",
18
+ "api_key",
19
+ "secret",
20
+ "password",
21
+ "credential",
22
+ "accesstoken",
23
+ "auth_token",
24
+ ]);
25
+ /** Run 前的运行时边界校验;拒绝 TypeScript 类型系统外传入的非法对象。 */
26
+ export function assertAgentSpec(value) {
27
+ const spec = requirePlainObject(value, "AgentSpec");
28
+ assertOnlyKeys(spec, SPEC_KEYS, "AgentSpec");
29
+ requireNonEmptyString(spec.id, "AgentSpec.id");
30
+ if (typeof spec.systemPrompt !== "string") {
31
+ configError("AgentSpec.systemPrompt must be a string.");
32
+ }
33
+ const model = requirePlainObject(spec.model, "AgentSpec.model");
34
+ assertOnlyKeys(model, MODEL_KEYS, "AgentSpec.model");
35
+ requireNonEmptyString(model.ref, "AgentSpec.model.ref");
36
+ if (!Array.isArray(spec.tools)) {
37
+ configError("AgentSpec.tools must be an array.");
38
+ }
39
+ const toolRefs = new Set();
40
+ for (const [index, binding] of spec.tools.entries()) {
41
+ const ref = assertBinding(binding, `AgentSpec.tools[${index}]`);
42
+ if (toolRefs.has(ref)) {
43
+ configError(`AgentSpec contains duplicate Tool ref ${ref}.`);
44
+ }
45
+ toolRefs.add(ref);
46
+ }
47
+ if (Object.hasOwn(spec, "context") && spec.context === undefined) {
48
+ configError("AgentSpec.context must not be undefined when present.");
49
+ }
50
+ if (spec.context !== undefined) {
51
+ assertBinding(spec.context, "AgentSpec.context");
52
+ }
53
+ if (Object.hasOwn(spec, "limits") && spec.limits === undefined) {
54
+ configError("AgentSpec.limits must not be undefined when present.");
55
+ }
56
+ if (spec.limits !== undefined) {
57
+ const limits = requirePlainObject(spec.limits, "AgentSpec.limits");
58
+ assertOnlyKeys(limits, LIMIT_KEYS, "AgentSpec.limits");
59
+ for (const key of LIMIT_KEYS) {
60
+ if (Object.hasOwn(limits, key)) {
61
+ const limit = limits[key];
62
+ if (!Number.isInteger(limit) || limit <= 0) {
63
+ configError(`AgentSpec.limits.${key} must be a positive integer.`);
64
+ }
65
+ if (key === "timeoutMs" && limit > MAX_TIMEOUT_MS) {
66
+ configError(`AgentSpec.limits.timeoutMs must not exceed ${MAX_TIMEOUT_MS}.`);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ if (Object.hasOwn(spec, "structuredOutput") && spec.structuredOutput === undefined) {
72
+ configError("AgentSpec.structuredOutput must not be undefined when present.");
73
+ }
74
+ if (spec.structuredOutput !== undefined) {
75
+ assertStructuredOutputConfig(spec.structuredOutput, "AgentSpec.structuredOutput");
76
+ }
77
+ }
78
+ function assertBinding(value, path) {
79
+ const binding = requirePlainObject(value, path);
80
+ assertOnlyKeys(binding, BINDING_KEYS, path);
81
+ const ref = requireNonEmptyString(binding.ref, `${path}.ref`);
82
+ if (Object.hasOwn(binding, "config")) {
83
+ assertJsonValue(binding.config, `${path}.config`, new WeakSet());
84
+ }
85
+ return ref;
86
+ }
87
+ function assertJsonValue(value, path, seen) {
88
+ if (value === null ||
89
+ typeof value === "string" ||
90
+ typeof value === "boolean" ||
91
+ (typeof value === "number" && Number.isFinite(value))) {
92
+ return;
93
+ }
94
+ if (typeof value !== "object") {
95
+ configError(`${path} must contain only JSON-safe values.`);
96
+ }
97
+ if (seen.has(value)) {
98
+ configError(`${path} must not contain circular references.`);
99
+ }
100
+ seen.add(value);
101
+ if (Array.isArray(value)) {
102
+ value.forEach((item, index) => assertJsonValue(item, `${path}[${index}]`, seen));
103
+ }
104
+ else {
105
+ const object = requirePlainObject(value, path);
106
+ for (const [key, item] of Object.entries(object)) {
107
+ if (SECRET_KEYS.has(key.toLowerCase())) {
108
+ configError(`${path} must not contain secrets (${key}).`);
109
+ }
110
+ assertJsonValue(item, `${path}.${key}`, seen);
111
+ }
112
+ }
113
+ seen.delete(value);
114
+ }
115
+ function requirePlainObject(value, path) {
116
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
117
+ configError(`${path} must be a plain object.`);
118
+ }
119
+ const prototype = Object.getPrototypeOf(value);
120
+ if (prototype !== Object.prototype && prototype !== null) {
121
+ configError(`${path} must be a plain object.`);
122
+ }
123
+ return value;
124
+ }
125
+ function assertOnlyKeys(value, allowed, path) {
126
+ for (const key of Reflect.ownKeys(value)) {
127
+ if (typeof key !== "string" || !allowed.has(key)) {
128
+ configError(`${path} contains unsupported field ${String(key)}.`);
129
+ }
130
+ }
131
+ }
132
+ function requireNonEmptyString(value, path) {
133
+ if (typeof value !== "string" || value.trim().length === 0) {
134
+ configError(`${path} must be a non-empty string.`);
135
+ }
136
+ return value;
137
+ }
138
+ function configError(message) {
139
+ throw new AtomsAgentError("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C;IACrD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,WAAW,CAAC,OAAe;IAClC,MAAM,IAAI,eAAe,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;AACrD,CAAC"}
@@ -0,0 +1,42 @@
1
+ import type { AgentMessage } from "@atoms-agent/llm";
2
+ /**
3
+ * AgentState —— 运行所需的最小状态(PRD §8.5)。
4
+ *
5
+ * v0.2(任务 V202)提供 JSON 导出/恢复;不承诺 Crash Consistency、
6
+ * 并发写入或副作用恢复。不保存 Function、Client、Secret 或 Active Operation。
7
+ */
8
+ export type AgentState = {
9
+ messages: AgentMessage[];
10
+ };
11
+ /** 当前 Snapshot 协议版本;不兼容版本在 restore 时被拒绝。 */
12
+ export declare const AGENT_STATE_SCHEMA_VERSION: 1;
13
+ /**
14
+ * 应用层可持久化的 JSON-safe 快照。
15
+ *
16
+ * 只包含消息历史与 schemaVersion。不包含 AgentSpec、Registry Binding、
17
+ * AbortSignal、Provider Client 或未完成的 Tool 操作。
18
+ */
19
+ export type AgentStateSnapshot = {
20
+ schemaVersion: typeof AGENT_STATE_SCHEMA_VERSION;
21
+ messages: AgentMessage[];
22
+ };
23
+ /**
24
+ * 导出 JSON-safe 状态快照(深拷贝)。
25
+ * 返回值可直接 `JSON.stringify` 并由应用层自行存储。
26
+ */
27
+ export declare function exportState(state: AgentState): AgentStateSnapshot;
28
+ /**
29
+ * 校验并恢复 AgentState。
30
+ *
31
+ * - 拒绝未知/不兼容 schemaVersion。
32
+ * - 校验每条 Message 的 role 与 Content Block 结构。
33
+ * - 校验 Tool Call/Result 的顺序、一一配对、ID 与名称。
34
+ * - 拒绝 Function、Class Instance、循环引用与非 JSON 值。
35
+ * - 不承诺 Crash Consistency、并发写或副作用恢复。
36
+ */
37
+ export declare function restoreState(snapshot: unknown): AgentState;
38
+ /** 运行时校验 Snapshot;通过后返回窄化后的类型。 */
39
+ export declare function assertAgentStateSnapshot(value: unknown): AgentStateSnapshot;
40
+ /** 验证消息结构以及 Assistant Tool Call 与紧随其后的 Tool Result 配对。 */
41
+ export declare function assertAgentMessageHistory(value: unknown, path: string): AgentMessage[];
42
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EAQb,MAAM,kBAAkB,CAAC;AAG1B;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,YAAY,EAAE,CAAC;CAC1B,CAAC;AAEF,4CAA4C;AAC5C,eAAO,MAAM,0BAA0B,EAAG,CAAU,CAAC;AAErD;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,aAAa,EAAE,OAAO,0BAA0B,CAAC;IACjD,QAAQ,EAAE,YAAY,EAAE,CAAC;CAC1B,CAAC;AAQF;;;GAGG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,kBAAkB,CAMjE;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,OAAO,GAAG,UAAU,CAK1D;AAED,kCAAkC;AAClC,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,OAAO,GAAG,kBAAkB,CAe3E;AAED,0DAA0D;AAC1D,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,EAAE,CAOtF"}
package/dist/state.js ADDED
@@ -0,0 +1,263 @@
1
+ import { AtomsAgentError } from "@atoms-agent/llm";
2
+ /** 当前 Snapshot 协议版本;不兼容版本在 restore 时被拒绝。 */
3
+ export const AGENT_STATE_SCHEMA_VERSION = 1;
4
+ const SNAPSHOT_KEYS = new Set(["schemaVersion", "messages"]);
5
+ const MESSAGE_KEYS = new Set(["role", "content"]);
6
+ const TEXT_BLOCK_KEYS = new Set(["type", "text"]);
7
+ const TOOL_CALL_KEYS = new Set(["type", "toolCallId", "toolName", "input"]);
8
+ const TOOL_RESULT_KEYS = new Set(["type", "toolCallId", "toolName", "content", "isError"]);
9
+ /**
10
+ * 导出 JSON-safe 状态快照(深拷贝)。
11
+ * 返回值可直接 `JSON.stringify` 并由应用层自行存储。
12
+ */
13
+ export function exportState(state) {
14
+ const messages = assertAgentMessageHistory(state.messages, "AgentState.messages");
15
+ return {
16
+ schemaVersion: AGENT_STATE_SCHEMA_VERSION,
17
+ messages,
18
+ };
19
+ }
20
+ /**
21
+ * 校验并恢复 AgentState。
22
+ *
23
+ * - 拒绝未知/不兼容 schemaVersion。
24
+ * - 校验每条 Message 的 role 与 Content Block 结构。
25
+ * - 校验 Tool Call/Result 的顺序、一一配对、ID 与名称。
26
+ * - 拒绝 Function、Class Instance、循环引用与非 JSON 值。
27
+ * - 不承诺 Crash Consistency、并发写或副作用恢复。
28
+ */
29
+ export function restoreState(snapshot) {
30
+ const validated = assertAgentStateSnapshot(snapshot);
31
+ return {
32
+ messages: structuredClone(validated.messages),
33
+ };
34
+ }
35
+ /** 运行时校验 Snapshot;通过后返回窄化后的类型。 */
36
+ export function assertAgentStateSnapshot(value) {
37
+ const snapshot = requirePlainObject(value, "AgentStateSnapshot");
38
+ assertOnlyKeys(snapshot, SNAPSHOT_KEYS, "AgentStateSnapshot");
39
+ if (snapshot.schemaVersion !== AGENT_STATE_SCHEMA_VERSION) {
40
+ configError(`AgentStateSnapshot.schemaVersion must be ${AGENT_STATE_SCHEMA_VERSION}; got ${String(snapshot.schemaVersion)}.`);
41
+ }
42
+ const messages = assertAgentMessageHistory(snapshot.messages, "AgentStateSnapshot.messages");
43
+ return {
44
+ schemaVersion: AGENT_STATE_SCHEMA_VERSION,
45
+ messages,
46
+ };
47
+ }
48
+ /** 验证消息结构以及 Assistant Tool Call 与紧随其后的 Tool Result 配对。 */
49
+ export function assertAgentMessageHistory(value, path) {
50
+ if (!Array.isArray(value)) {
51
+ configError(`${path} must be an array.`);
52
+ }
53
+ const messages = value.map((message, index) => assertAgentMessage(message, `${path}[${index}]`));
54
+ assertToolResultPairing(messages, path);
55
+ return messages;
56
+ }
57
+ function assertAgentMessage(value, path) {
58
+ const message = requirePlainObject(value, path);
59
+ assertOnlyKeys(message, MESSAGE_KEYS, path);
60
+ if (message.role === "user") {
61
+ return assertUserMessage(message, path);
62
+ }
63
+ if (message.role === "assistant") {
64
+ return assertAssistantMessage(message, path);
65
+ }
66
+ if (message.role === "tool") {
67
+ return assertToolMessage(message, path);
68
+ }
69
+ configError(`${path}.role must be "user", "assistant", or "tool".`);
70
+ }
71
+ function assertUserMessage(message, path) {
72
+ if (!Array.isArray(message.content) || message.content.length === 0) {
73
+ configError(`${path}.content must be a non-empty array of text blocks.`);
74
+ }
75
+ return {
76
+ role: "user",
77
+ content: message.content.map((block, index) => assertTextBlock(block, `${path}.content[${index}]`)),
78
+ };
79
+ }
80
+ function assertAssistantMessage(message, path) {
81
+ if (!Array.isArray(message.content)) {
82
+ configError(`${path}.content must be an array of text/tool_call blocks.`);
83
+ }
84
+ return {
85
+ role: "assistant",
86
+ content: message.content.map((block, index) => assertAssistantBlock(block, `${path}.content[${index}]`)),
87
+ };
88
+ }
89
+ function assertToolResultPairing(messages, path) {
90
+ let pending;
91
+ for (const [index, message] of messages.entries()) {
92
+ if (pending !== undefined) {
93
+ if (message.role !== "tool") {
94
+ configError(`${path}[${pending.index}] Tool Calls must be followed immediately by one Tool Result message.`);
95
+ }
96
+ if (message.content.length !== pending.calls.length) {
97
+ configError(`${path}[${index}] must contain exactly one result for each preceding Tool Call.`);
98
+ }
99
+ const resultIds = new Set();
100
+ for (const result of message.content) {
101
+ const call = pending.byId.get(result.toolCallId);
102
+ if (call === undefined ||
103
+ resultIds.has(result.toolCallId) ||
104
+ call.toolName !== result.toolName) {
105
+ configError(`${path}[${index}] contains an unmatched or duplicate Tool Result.`);
106
+ }
107
+ resultIds.add(result.toolCallId);
108
+ }
109
+ pending = undefined;
110
+ continue;
111
+ }
112
+ if (message.role === "tool") {
113
+ configError(`${path}[${index}] is a Tool Result message without preceding Tool Calls.`);
114
+ }
115
+ if (message.role !== "assistant") {
116
+ continue;
117
+ }
118
+ const calls = message.content.filter((block) => block.type === "tool_call");
119
+ if (calls.length === 0) {
120
+ continue;
121
+ }
122
+ const byId = new Map();
123
+ for (const call of calls) {
124
+ if (byId.has(call.toolCallId)) {
125
+ configError(`${path}[${index}] contains duplicate Tool Call IDs.`);
126
+ }
127
+ byId.set(call.toolCallId, call);
128
+ }
129
+ pending = { index, calls, byId };
130
+ }
131
+ if (pending !== undefined) {
132
+ configError(`${path}[${pending.index}] contains Tool Calls without matching Tool Results.`);
133
+ }
134
+ }
135
+ function assertToolMessage(message, path) {
136
+ if (!Array.isArray(message.content) || message.content.length === 0) {
137
+ configError(`${path}.content must be a non-empty array of tool_result blocks.`);
138
+ }
139
+ return {
140
+ role: "tool",
141
+ content: message.content.map((block, index) => assertToolResultBlock(block, `${path}.content[${index}]`)),
142
+ };
143
+ }
144
+ function assertAssistantBlock(value, path) {
145
+ const block = requirePlainObject(value, path);
146
+ if (block.type === "text") {
147
+ return assertTextBlock(block, path);
148
+ }
149
+ if (block.type === "tool_call") {
150
+ return assertToolCallBlock(block, path);
151
+ }
152
+ configError(`${path}.type must be "text" or "tool_call".`);
153
+ }
154
+ function assertTextBlock(value, path) {
155
+ const block = requirePlainObject(value, path);
156
+ assertOnlyKeys(block, TEXT_BLOCK_KEYS, path);
157
+ if (block.type !== "text") {
158
+ configError(`${path}.type must be "text".`);
159
+ }
160
+ if (typeof block.text !== "string") {
161
+ configError(`${path}.text must be a string.`);
162
+ }
163
+ return { type: "text", text: block.text };
164
+ }
165
+ function assertToolCallBlock(value, path) {
166
+ const block = requirePlainObject(value, path);
167
+ assertOnlyKeys(block, TOOL_CALL_KEYS, path);
168
+ if (block.type !== "tool_call") {
169
+ configError(`${path}.type must be "tool_call".`);
170
+ }
171
+ const toolCallId = requireNonEmptyString(block.toolCallId, `${path}.toolCallId`);
172
+ const toolName = requireNonEmptyString(block.toolName, `${path}.toolName`);
173
+ assertJsonValue(block.input, `${path}.input`, new WeakSet());
174
+ return {
175
+ type: "tool_call",
176
+ toolCallId,
177
+ toolName,
178
+ input: block.input,
179
+ };
180
+ }
181
+ function assertToolResultBlock(value, path) {
182
+ const block = requirePlainObject(value, path);
183
+ assertOnlyKeys(block, TOOL_RESULT_KEYS, path);
184
+ if (block.type !== "tool_result") {
185
+ configError(`${path}.type must be "tool_result".`);
186
+ }
187
+ const toolCallId = requireNonEmptyString(block.toolCallId, `${path}.toolCallId`);
188
+ const toolName = requireNonEmptyString(block.toolName, `${path}.toolName`);
189
+ if (typeof block.content !== "string") {
190
+ configError(`${path}.content must be a string.`);
191
+ }
192
+ const content = block.content;
193
+ if (!Object.hasOwn(block, "isError")) {
194
+ return {
195
+ type: "tool_result",
196
+ toolCallId,
197
+ toolName,
198
+ content,
199
+ };
200
+ }
201
+ if (typeof block.isError !== "boolean") {
202
+ configError(`${path}.isError must be a boolean when present.`);
203
+ }
204
+ return {
205
+ type: "tool_result",
206
+ toolCallId,
207
+ toolName,
208
+ content,
209
+ isError: block.isError,
210
+ };
211
+ }
212
+ function assertJsonValue(value, path, seen) {
213
+ if (value === null ||
214
+ typeof value === "string" ||
215
+ typeof value === "boolean" ||
216
+ (typeof value === "number" && Number.isFinite(value))) {
217
+ return;
218
+ }
219
+ if (typeof value !== "object") {
220
+ configError(`${path} must contain only JSON-safe values.`);
221
+ }
222
+ if (seen.has(value)) {
223
+ configError(`${path} must not contain circular references.`);
224
+ }
225
+ seen.add(value);
226
+ if (Array.isArray(value)) {
227
+ value.forEach((item, index) => assertJsonValue(item, `${path}[${index}]`, seen));
228
+ }
229
+ else {
230
+ const object = requirePlainObject(value, path);
231
+ for (const [key, item] of Object.entries(object)) {
232
+ assertJsonValue(item, `${path}.${key}`, seen);
233
+ }
234
+ }
235
+ seen.delete(value);
236
+ }
237
+ function requirePlainObject(value, path) {
238
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
239
+ configError(`${path} must be a plain object.`);
240
+ }
241
+ const prototype = Object.getPrototypeOf(value);
242
+ if (prototype !== Object.prototype && prototype !== null) {
243
+ configError(`${path} must be a plain object.`);
244
+ }
245
+ return value;
246
+ }
247
+ function assertOnlyKeys(value, allowed, path) {
248
+ for (const key of Reflect.ownKeys(value)) {
249
+ if (typeof key !== "string" || !allowed.has(key)) {
250
+ configError(`${path} contains unsupported field ${String(key)}.`);
251
+ }
252
+ }
253
+ }
254
+ function requireNonEmptyString(value, path) {
255
+ if (typeof value !== "string" || value.trim().length === 0) {
256
+ configError(`${path} must be a non-empty string.`);
257
+ }
258
+ return value;
259
+ }
260
+ function configError(message) {
261
+ throw new AtomsAgentError("CONFIG_ERROR", message);
262
+ }
263
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAYnD,4CAA4C;AAC5C,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAU,CAAC;AAarD,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC,CAAC;AAC7D,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;AAClD,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAClD,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC;AAC5E,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;AAE3F;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,KAAiB;IAC3C,MAAM,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,GAAG,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,KAAc,EAAE,IAAY;IACzD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3D,WAAW,CAAC,GAAG,IAAI,8BAA8B,CAAC,CAAC;IACrD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,WAAW,CAAC,OAAe;IAClC,MAAM,IAAI,eAAe,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;AACrD,CAAC"}
@@ -0,0 +1,41 @@
1
+ import type { JsonSchema, JsonValue, StructuredOutputRequest } from "@atoms-agent/llm";
2
+ import { AtomsAgentError } from "@atoms-agent/llm";
3
+ export type StructuredOutputConfig = {
4
+ /** Provider Wire Format 使用的稳定名称;省略时为 agent_output。 */
5
+ name?: string;
6
+ description?: string;
7
+ schema: JsonSchema;
8
+ };
9
+ export type StructuredOutputDiagnostic = {
10
+ instancePath: string;
11
+ schemaPath: string;
12
+ keyword: string;
13
+ message: string;
14
+ };
15
+ export type StructuredOutputResult = {
16
+ valid: true;
17
+ value: JsonValue;
18
+ diagnostics: StructuredOutputDiagnostic[];
19
+ repairAttempted: boolean;
20
+ } | {
21
+ valid: false;
22
+ diagnostics: StructuredOutputDiagnostic[];
23
+ repairAttempted: boolean;
24
+ };
25
+ export type StructuredOutputFailure = Extract<StructuredOutputResult, {
26
+ valid: false;
27
+ }>;
28
+ export type StructuredOutputRuntime = {
29
+ request: StructuredOutputRequest;
30
+ promptInstruction: string;
31
+ validate(text: string, repairAttempted: boolean): StructuredOutputResult;
32
+ repairPrompt(diagnostics: readonly StructuredOutputDiagnostic[]): string;
33
+ };
34
+ export declare function assertStructuredOutputConfig(value: unknown, path?: string): asserts value is StructuredOutputConfig;
35
+ export declare function createStructuredOutputRuntime(config: StructuredOutputConfig): StructuredOutputRuntime;
36
+ export declare function createStructuredOutputFailure(diagnostics: readonly StructuredOutputDiagnostic[], repairAttempted: boolean): StructuredOutputFailure;
37
+ export declare class StructuredOutputValidationError extends AtomsAgentError {
38
+ readonly result: StructuredOutputFailure;
39
+ constructor(result: StructuredOutputFailure);
40
+ }
41
+ //# sourceMappingURL=structured-output.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structured-output.d.ts","sourceRoot":"","sources":["../src/structured-output.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AACvF,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAInD,MAAM,MAAM,sBAAsB,GAAG;IACnC,sDAAsD;IACtD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,UAAU,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAC9B;IACE,KAAK,EAAE,IAAI,CAAC;IACZ,KAAK,EAAE,SAAS,CAAC;IACjB,WAAW,EAAE,0BAA0B,EAAE,CAAC;IAC1C,eAAe,EAAE,OAAO,CAAC;CAC1B,GACD;IACE,KAAK,EAAE,KAAK,CAAC;IACb,WAAW,EAAE,0BAA0B,EAAE,CAAC;IAC1C,eAAe,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEN,MAAM,MAAM,uBAAuB,GAAG,OAAO,CAAC,sBAAsB,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC,CAAC;AAExF,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,EAAE,uBAAuB,CAAC;IACjC,iBAAiB,EAAE,MAA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