@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
@@ -0,0 +1,170 @@
1
+ import { AtomsAgentError } from "@atoms-agent/llm";
2
+ import { Ajv } from "ajv";
3
+ const ajv = new Ajv({ allErrors: true, strict: true, addUsedSchema: false });
4
+ const CONFIG_KEYS = new Set(["name", "description", "schema"]);
5
+ export function assertStructuredOutputConfig(value, path = "Structured Output") {
6
+ const config = requirePlainObject(value, path);
7
+ assertOnlyKeys(config, CONFIG_KEYS, path);
8
+ if (Object.hasOwn(config, "name")) {
9
+ if (typeof config.name !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(config.name)) {
10
+ configError(`${path}.name must match [A-Za-z0-9_-]{1,64}.`);
11
+ }
12
+ }
13
+ if (Object.hasOwn(config, "description")) {
14
+ if (typeof config.description !== "string" || config.description.trim().length === 0) {
15
+ configError(`${path}.description must be a non-empty string.`);
16
+ }
17
+ }
18
+ const schema = requirePlainObject(config.schema, `${path}.schema`);
19
+ assertJsonSafe(schema, `${path}.schema`, new WeakSet());
20
+ }
21
+ export function createStructuredOutputRuntime(config) {
22
+ assertStructuredOutputConfig(config);
23
+ const request = {
24
+ name: config.name ?? "agent_output",
25
+ ...(config.description === undefined ? {} : { description: config.description }),
26
+ schema: structuredClone(config.schema),
27
+ };
28
+ let validator;
29
+ try {
30
+ validator = ajv.compile(request.schema);
31
+ }
32
+ catch (cause) {
33
+ throw new AtomsAgentError("CONFIG_ERROR", "Structured Output schema is invalid.", { cause });
34
+ }
35
+ return {
36
+ request,
37
+ promptInstruction: buildPromptInstruction(request),
38
+ validate(text, repairAttempted) {
39
+ let parsed;
40
+ try {
41
+ parsed = JSON.parse(text);
42
+ }
43
+ catch {
44
+ return createStructuredOutputFailure([diagnostic("parse", "The final response is not valid JSON.")], repairAttempted);
45
+ }
46
+ if (!isJsonValue(parsed)) {
47
+ return createStructuredOutputFailure([diagnostic("json_safe", "The parsed response is not a JSON-safe value.")], repairAttempted);
48
+ }
49
+ if (!validator(parsed)) {
50
+ return createStructuredOutputFailure((validator.errors ?? []).map(toDiagnostic), repairAttempted);
51
+ }
52
+ return {
53
+ valid: true,
54
+ value: structuredClone(parsed),
55
+ diagnostics: [],
56
+ repairAttempted,
57
+ };
58
+ },
59
+ repairPrompt(diagnostics) {
60
+ const details = diagnostics
61
+ .map((item) => `- ${item.instancePath.length === 0 ? "/" : item.instancePath}: ${item.message} (${item.keyword})`)
62
+ .join("\n");
63
+ return [
64
+ "Your previous final response did not satisfy the required JSON Schema.",
65
+ "Correct it now. Return only the corrected JSON value, with no Markdown or explanation.",
66
+ "Validation diagnostics:",
67
+ details,
68
+ ].join("\n");
69
+ },
70
+ };
71
+ }
72
+ export function createStructuredOutputFailure(diagnostics, repairAttempted) {
73
+ return {
74
+ valid: false,
75
+ diagnostics: structuredClone([...diagnostics]),
76
+ repairAttempted,
77
+ };
78
+ }
79
+ export class StructuredOutputValidationError extends AtomsAgentError {
80
+ result;
81
+ constructor(result) {
82
+ super("OUTPUT_VALIDATION_ERROR", "Structured Output remained invalid after one repair attempt.");
83
+ this.name = "StructuredOutputValidationError";
84
+ this.result = structuredClone(result);
85
+ }
86
+ }
87
+ function buildPromptInstruction(request) {
88
+ return [
89
+ "Structured Output requirement:",
90
+ "You may call available tools before answering. When producing the final answer, return only one JSON value that matches the schema below.",
91
+ "Do not wrap the JSON in Markdown and do not add explanatory text.",
92
+ ...(request.description === undefined ? [] : [`Purpose: ${request.description}`]),
93
+ `Schema name: ${request.name}`,
94
+ JSON.stringify(request.schema),
95
+ ].join("\n");
96
+ }
97
+ function toDiagnostic(error) {
98
+ return {
99
+ instancePath: error.instancePath,
100
+ schemaPath: error.schemaPath,
101
+ keyword: error.keyword,
102
+ message: error.message ?? "JSON Schema validation failed.",
103
+ };
104
+ }
105
+ function diagnostic(keyword, message) {
106
+ return { instancePath: "", schemaPath: "", keyword, message };
107
+ }
108
+ function isJsonValue(value) {
109
+ if (value === null ||
110
+ typeof value === "string" ||
111
+ typeof value === "boolean" ||
112
+ (typeof value === "number" && Number.isFinite(value))) {
113
+ return true;
114
+ }
115
+ if (Array.isArray(value)) {
116
+ return value.every(isJsonValue);
117
+ }
118
+ if (typeof value !== "object") {
119
+ return false;
120
+ }
121
+ const prototype = Object.getPrototypeOf(value);
122
+ return ((prototype === Object.prototype || prototype === null) &&
123
+ Object.values(value).every(isJsonValue));
124
+ }
125
+ function assertJsonSafe(value, path, seen) {
126
+ if (value === null ||
127
+ typeof value === "string" ||
128
+ typeof value === "boolean" ||
129
+ (typeof value === "number" && Number.isFinite(value))) {
130
+ return;
131
+ }
132
+ if (typeof value !== "object") {
133
+ configError(`${path} must contain only JSON-safe values.`);
134
+ }
135
+ if (seen.has(value)) {
136
+ configError(`${path} must not contain circular references.`);
137
+ }
138
+ seen.add(value);
139
+ if (Array.isArray(value)) {
140
+ value.forEach((item, index) => assertJsonSafe(item, `${path}[${index}]`, seen));
141
+ }
142
+ else {
143
+ const object = requirePlainObject(value, path);
144
+ for (const [key, item] of Object.entries(object)) {
145
+ assertJsonSafe(item, `${path}.${key}`, seen);
146
+ }
147
+ }
148
+ seen.delete(value);
149
+ }
150
+ function requirePlainObject(value, path) {
151
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
152
+ configError(`${path} must be a plain object.`);
153
+ }
154
+ const prototype = Object.getPrototypeOf(value);
155
+ if (prototype !== Object.prototype && prototype !== null) {
156
+ configError(`${path} must be a plain object.`);
157
+ }
158
+ return value;
159
+ }
160
+ function assertOnlyKeys(value, allowed, path) {
161
+ for (const key of Reflect.ownKeys(value)) {
162
+ if (typeof key !== "string" || !allowed.has(key)) {
163
+ configError(`${path} contains unsupported field ${String(key)}.`);
164
+ }
165
+ }
166
+ }
167
+ function configError(message) {
168
+ throw new AtomsAgentError("CONFIG_ERROR", message);
169
+ }
170
+ //# sourceMappingURL=structured-output.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"structured-output.js","sourceRoot":"","sources":["../src/structured-output.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAuC1B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,CAAC;AAC7E,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC;AAE/D,MAAM,UAAU,4BAA4B,CAC1C,KAAc,EACd,IAAI,GAAG,mBAAmB;IAE1B,MAAM,MAAM,GAAG,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC/C,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;IAE1C,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;QAClC,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YAClF,WAAW,CAAC,GAAG,IAAI,uCAAuC,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAAE,CAAC;QACzC,IAAI,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ,IAAI,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACrF,WAAW,CAAC,GAAG,IAAI,0CAA0C,CAAC,CAA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atoms-agent/llm";
2
+ /**
3
+ * Tool 副作用声明。影响并发与(未来的)恢复语义:
4
+ * v0.1 中 Tool 一律串行执行,该字段主要供 authorize 策略使用。
5
+ */
6
+ export type ToolSideEffect = "none" | "idempotent" | "non_idempotent";
7
+ /** 传给 Tool Execute 的运行时上下文。 */
8
+ export type ToolContext = {
9
+ toolCallId: string;
10
+ /** Run 级取消/超时信号,Tool 实现必须响应(AGENTS.md §12)。 */
11
+ signal: AbortSignal;
12
+ /** AgentSpec Tool Binding 的 JSON-safe 配置。 */
13
+ config?: JsonValue;
14
+ };
15
+ export type ToolDefinition<TInput extends JsonValue = JsonValue> = {
16
+ name: string;
17
+ description: string;
18
+ /** 可选;未指定时 Runtime 视为 "0.0.0"(AGENTS.md §9)。 */
19
+ version?: string;
20
+ inputSchema: JsonSchema;
21
+ sideEffect: ToolSideEffect;
22
+ /**
23
+ * 只在输入通过 JSON Schema 校验、且 authorize() 返回 allow 后被调用。
24
+ * 返回值会被字符串化并经过大小治理后写入 Tool Result。
25
+ * 失败应抛出异常,由 Runtime 转为分类 Tool Error,不要返回伪成功文本。
26
+ */
27
+ execute(input: TInput, context: ToolContext): Promise<JsonValue>;
28
+ };
29
+ export declare function defineTool<TInput extends JsonValue = JsonValue>(definition: ToolDefinition<TInput>): ToolDefinition<TInput>;
30
+ /** 一次待授权的 Tool 调用。input 已通过 schema 校验。 */
31
+ export type ToolCallRequest = {
32
+ toolCallId: string;
33
+ tool: Pick<ToolDefinition, "name" | "description" | "version" | "sideEffect">;
34
+ input: JsonValue;
35
+ config?: JsonValue;
36
+ signal: AbortSignal;
37
+ };
38
+ export type AuthorizeDecision = "allow" | "deny";
39
+ /**
40
+ * 统一权限回调(PRD TOOL-003)。deny 时 Tool 不执行,
41
+ * Runtime 生成带 AUTHORIZATION_ERROR 的 Tool Error Result 回填模型。
42
+ * 未提供 authorize 时的默认策略:仅 sideEffect === "none" 允许执行。
43
+ */
44
+ export type AuthorizeFn = (request: ToolCallRequest) => AuthorizeDecision | Promise<AuthorizeDecision>;
45
+ export type ToolOutputLimits = {
46
+ maxCharacters?: number;
47
+ maxBytes?: number;
48
+ maxEstimatedTokens?: number;
49
+ };
50
+ export declare const DEFAULT_TOOL_OUTPUT_LIMITS: Required<ToolOutputLimits>;
51
+ export type GovernedToolOutput = {
52
+ content: string;
53
+ truncated: boolean;
54
+ };
55
+ export type ToolResultErrorCode = Extract<AgentErrorCode, "TOOL_VALIDATION_ERROR" | "TOOL_EXECUTION_ERROR" | "AUTHORIZATION_ERROR">;
56
+ export type ToolRuntimeResult = {
57
+ toolCallId: string;
58
+ toolName: string;
59
+ content: string;
60
+ isError: boolean;
61
+ truncated: boolean;
62
+ errorCode?: ToolResultErrorCode;
63
+ };
64
+ export type ExecuteToolCallOptions = {
65
+ signal: AbortSignal;
66
+ authorize?: AuthorizeFn;
67
+ config?: JsonValue;
68
+ outputLimits?: ToolOutputLimits;
69
+ };
70
+ /** 默认权限:无副作用 Tool 自动允许,其余拒绝。 */
71
+ export declare const defaultAuthorize: AuthorizeFn;
72
+ /** C102 Tool Runtime;Loop 只调用该入口,保证执行顺序与错误配对一致。 */
73
+ export declare function executeToolCall<TInput extends JsonValue>(tool: ToolDefinition<TInput>, call: ToolCallBlock, options: ExecuteToolCallOptions): Promise<ToolRuntimeResult>;
74
+ export declare function truncateToolOutput(content: string, configuredLimits?: ToolOutputLimits): GovernedToolOutput;
75
+ //# sourceMappingURL=tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAO7F;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,YAAY,GAAG,gBAAgB,CAAC;AAEtE,+BAA+B;AAC/B,MAAM,MAAM,WAAW,GAAG;IACxB,UAAU,EAAE,MAAM,CAA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F,eAAO,MAAM,0BAA0B,EAAE,QAAQ,CAAC,gBAAgB,CAIhE,CAAC;AAEH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG,OAAO,CACvC,cAAc,EACd,uBAAuB,GAAG,sBAAsB,GAAG,qBAAqB,CACzE,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,mBAAmB,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,WAAW,CAAC;IACpB,SAAS,CAAC,EAAE,WAAW,CAAC;IACxB,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,YAAY,CAAC,EAAE,gBAAgB,CAAC;CACjC,CAAC;AAEF,gCAAgC;AAChC,eAAO,MAAM,gBAAgB,EAAE,WACgB,CAAC;AAEhD,mDAAmD;AACnD,wBAAsB,eAAe,CAAC,MAAM,SAAS,SAAS,EAC5D,IAAI,EAAE,cAAc,CAAC,MAAM,CAAC,EAC5B,IAAI,EAAE,aAAa,EACnB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,iBAAiB,CAAC,CAiF5B;AAKD,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,gBAAgB,CAAC,EAAE,gBAAgB,GAClC,kBAAkB,CAkCpB"}
package/dist/tool.js ADDED
@@ -0,0 +1,206 @@
1
+ import { AtomsAgentError } from "@atoms-agent/llm";
2
+ import { Ajv } from "ajv";
3
+ import { awaitWithAbort } from "./internal/abort.js";
4
+ /**
5
+ * 定义一个 Tool(任务 C102 接入校验与执行管线)。
6
+ *
7
+ * defineTool 本身只做定义收敛与冻结,不做 IO。
8
+ */
9
+ const ajv = new Ajv({ allErrors: true, strict: true, addUsedSchema: false });
10
+ const validators = new WeakMap();
11
+ export function defineTool(definition) {
12
+ if (definition.name.trim().length === 0) {
13
+ throw new AtomsAgentError("CONFIG_ERROR", "Tool name must not be empty.");
14
+ }
15
+ if (definition.description.trim().length === 0) {
16
+ throw new AtomsAgentError("CONFIG_ERROR", `Tool ${definition.name} requires a description.`);
17
+ }
18
+ const normalized = {
19
+ ...definition,
20
+ inputSchema: deepFreezeJson(structuredClone(definition.inputSchema)),
21
+ };
22
+ const tool = Object.freeze(normalized);
23
+ validatorFor(tool);
24
+ return tool;
25
+ }
26
+ export const DEFAULT_TOOL_OUTPUT_LIMITS = Object.freeze({
27
+ maxCharacters: 16_000,
28
+ maxBytes: 64_000,
29
+ maxEstimatedTokens: 4_000,
30
+ });
31
+ /** 默认权限:无副作用 Tool 自动允许,其余拒绝。 */
32
+ export const defaultAuthorize = ({ tool }) => tool.sideEffect === "none" ? "allow" : "deny";
33
+ /** C102 Tool Runtime;Loop 只调用该入口,保证执行顺序与错误配对一致。 */
34
+ export async function executeToolCall(tool, call, options) {
35
+ throwIfAborted(options.signal);
36
+ const limits = normalizeOutputLimits(options.outputLimits);
37
+ const validate = validatorFor(tool);
38
+ if (!validate(call.input)) {
39
+ return errorResult(tool, call, "TOOL_VALIDATION_ERROR", `Tool input is invalid: ${formatValidationErrors(validate.errors)}`, limits);
40
+ }
41
+ throwIfAborted(options.signal);
42
+ let decision;
43
+ try {
44
+ decision = await awaitWithAbort(() => (options.authorize ?? defaultAuthorize)({
45
+ toolCallId: call.toolCallId,
46
+ tool: {
47
+ name: tool.name,
48
+ description: tool.description,
49
+ version: tool.version ?? "0.0.0",
50
+ sideEffect: tool.sideEffect,
51
+ },
52
+ input: structuredClone(call.input),
53
+ ...(options.config === undefined ? {} : { config: structuredClone(options.config) }),
54
+ signal: options.signal,
55
+ }), options.signal, "Tool authorization was cancelled.");
56
+ }
57
+ catch (cause) {
58
+ throwIfAborted(options.signal);
59
+ return errorResult(tool, call, "AUTHORIZATION_ERROR", "Tool authorization failed.", limits, cause);
60
+ }
61
+ throwIfAborted(options.signal);
62
+ if (decision === "deny") {
63
+ return errorResult(tool, call, "AUTHORIZATION_ERROR", "Tool execution was denied.", limits);
64
+ }
65
+ if (decision !== "allow") {
66
+ return errorResult(tool, call, "AUTHORIZATION_ERROR", "Tool authorization returned an invalid decision.", limits);
67
+ }
68
+ throwIfAborted(options.signal);
69
+ try {
70
+ const output = await tool.execute(call.input, {
71
+ toolCallId: call.toolCallId,
72
+ signal: options.signal,
73
+ ...(options.config === undefined ? {} : { config: structuredClone(options.config) }),
74
+ });
75
+ throwIfAborted(options.signal);
76
+ const governed = truncateToolOutput(serializeToolOutput(output), limits);
77
+ return {
78
+ toolCallId: call.toolCallId,
79
+ toolName: tool.name,
80
+ content: governed.content,
81
+ isError: false,
82
+ truncated: governed.truncated,
83
+ };
84
+ }
85
+ catch (cause) {
86
+ throwIfAborted(options.signal);
87
+ return errorResult(tool, call, "TOOL_EXECUTION_ERROR", "Tool execution failed.", limits, cause);
88
+ }
89
+ }
90
+ const TRUNCATION_NOTICE = "\n...[tool output truncated]";
91
+ const textEncoder = new TextEncoder();
92
+ export function truncateToolOutput(content, configuredLimits) {
93
+ const limits = normalizeOutputLimits(configuredLimits);
94
+ if (fitsOutputLimits(content, limits)) {
95
+ return { content, truncated: false };
96
+ }
97
+ const characters = Array.from(content);
98
+ let low = 0;
99
+ let high = characters.length;
100
+ while (low < high) {
101
+ const middle = Math.ceil((low + high) / 2);
102
+ if (fitsOutputLimits(characters.slice(0, middle).join("") + TRUNCATION_NOTICE, limits)) {
103
+ low = middle;
104
+ }
105
+ else {
106
+ high = middle - 1;
107
+ }
108
+ }
109
+ const candidate = characters.slice(0, low).join("") + TRUNCATION_NOTICE;
110
+ if (fitsOutputLimits(candidate, limits)) {
111
+ return { content: candidate, truncated: true };
112
+ }
113
+ const noticeCharacters = Array.from(TRUNCATION_NOTICE);
114
+ while (noticeCharacters.length > 0) {
115
+ const notice = noticeCharacters.join("");
116
+ if (fitsOutputLimits(notice, limits)) {
117
+ return { content: notice, truncated: true };
118
+ }
119
+ noticeCharacters.pop();
120
+ }
121
+ return { content: "", truncated: true };
122
+ }
123
+ function validatorFor(tool) {
124
+ const existing = validators.get(tool);
125
+ if (existing !== undefined) {
126
+ return existing;
127
+ }
128
+ try {
129
+ const validator = ajv.compile(tool.inputSchema);
130
+ validators.set(tool, validator);
131
+ return validator;
132
+ }
133
+ catch (cause) {
134
+ throw new AtomsAgentError("CONFIG_ERROR", `Tool ${tool.name} has an invalid input schema.`, {
135
+ cause,
136
+ });
137
+ }
138
+ }
139
+ function errorResult(tool, call, errorCode, message, limits, cause) {
140
+ const error = new AtomsAgentError(errorCode, message, { cause });
141
+ const governed = truncateToolOutput(`[${error.code}] ${error.message}`, limits);
142
+ return {
143
+ toolCallId: call.toolCallId,
144
+ toolName: tool.name,
145
+ content: governed.content,
146
+ isError: true,
147
+ truncated: governed.truncated,
148
+ errorCode,
149
+ };
150
+ }
151
+ function normalizeOutputLimits(configuredLimits) {
152
+ const limits = {
153
+ ...DEFAULT_TOOL_OUTPUT_LIMITS,
154
+ ...configuredLimits,
155
+ };
156
+ for (const [name, value] of Object.entries(limits)) {
157
+ if (!Number.isInteger(value) || value <= 0) {
158
+ throw new AtomsAgentError("CONFIG_ERROR", `${name} must be a positive integer.`);
159
+ }
160
+ }
161
+ return limits;
162
+ }
163
+ function fitsOutputLimits(content, limits) {
164
+ const characters = Array.from(content).length;
165
+ const bytes = textEncoder.encode(content).byteLength;
166
+ const estimatedTokens = Math.ceil(bytes / 4);
167
+ return (characters <= limits.maxCharacters &&
168
+ bytes <= limits.maxBytes &&
169
+ estimatedTokens <= limits.maxEstimatedTokens);
170
+ }
171
+ function serializeToolOutput(output) {
172
+ if (typeof output === "string") {
173
+ return output;
174
+ }
175
+ const serialized = JSON.stringify(output);
176
+ if (serialized === undefined) {
177
+ throw new TypeError("Tool output is not JSON serializable.");
178
+ }
179
+ return serialized;
180
+ }
181
+ function formatValidationErrors(errors) {
182
+ if (errors === null || errors === undefined || errors.length === 0) {
183
+ return "schema validation failed";
184
+ }
185
+ return errors
186
+ .map((error) => `${error.instancePath || "/"} ${error.message ?? error.keyword}`)
187
+ .join("; ");
188
+ }
189
+ function throwIfAborted(signal) {
190
+ if (signal.aborted) {
191
+ throw new AtomsAgentError("CANCELLED", "Tool execution was cancelled.", {
192
+ cause: signal.reason,
193
+ });
194
+ }
195
+ }
196
+ function deepFreezeJson(value) {
197
+ if (value !== null && typeof value === "object") {
198
+ const children = Array.isArray(value) ? value : Object.values(value);
199
+ for (const child of children) {
200
+ deepFreezeJson(child);
201
+ }
202
+ Object.freeze(value);
203
+ }
204
+ return value;
205
+ }
206
+ //# sourceMappingURL=tool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.js","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAG1B,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAgCrD;;;;GAIG;AACH,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC,CAAC;AAC7E,MAAM,UAAU,GAAG,IAAI,OAAO,EAA4B,CAAC;AAE3D,MAAM,UAAU,UAAU,CACxB,UAAkC;IAElC,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxC,MAAM,IAAI,eAAe,CAAC,cAAc,EAAE,8BAA8B,CAAC,CAAC;IAC5E,CAAC;IACD,IAAI,UAAU,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC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IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnE,OAAO,0BAA0B,CAAC;IACpC,CAAC;IACD,OAAO,MAAM;SACV,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,YAAY,IAAI,GAAG,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;SAChF,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC;AAED,SAAS,cAAc,CAAC,MAAmB;IACzC,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACnB,MAAM,IAAI,eAAe,CAAC,WAAW,EAAE,+BAA+B,EAAE;YACtE,KAAK,EAAE,MAAM,CAAC,MAAM;SACrB,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAED,SAAS,cAAc,CAAsB,KAAQ;IACnD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAChD,MAAM,QAAQ,GAAgB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAClF,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC7B,cAAc,CAAC,KAAK,CAAC,CAAC;QACxB,CAAC;QACD,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@atoms-agent/core",
3
+ "version": "0.3.0",
4
+ "type": "module",
5
+ "description": "Agent loop, tool runtime, event stream, context strategy, limits and cancellation",
6
+ "license": "MIT",
7
+ "author": "atoms_agent contributors",
8
+ "homepage": "https://github.com/Fufu-0101/atoms_agent#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Fufu-0101/atoms_agent.git",
12
+ "directory": "packages/core"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/Fufu-0101/atoms_agent/issues"
16
+ },
17
+ "keywords": [
18
+ "agent",
19
+ "agent-loop",
20
+ "tools",
21
+ "llm",
22
+ "typescript",
23
+ "sdk"
24
+ ],
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "engines": {
40
+ "node": ">=22.13"
41
+ },
42
+ "dependencies": {
43
+ "@atoms-agent/llm": "0.3.0",
44
+ "ajv": "^8.17.1"
45
+ },
46
+ "scripts": {
47
+ "test": "vitest run",
48
+ "build": "tsc -b"
49
+ }
50
+ }