@ddlqhd/agent-sdk 0.1.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 (50) hide show
  1. package/README.md +53 -0
  2. package/dist/chunk-5QMA2YBY.cjs +2880 -0
  3. package/dist/chunk-5QMA2YBY.cjs.map +1 -0
  4. package/dist/chunk-5Y56A64C.cjs +5 -0
  5. package/dist/chunk-5Y56A64C.cjs.map +1 -0
  6. package/dist/chunk-A3S3AGE3.js +3 -0
  7. package/dist/chunk-A3S3AGE3.js.map +1 -0
  8. package/dist/chunk-CNSGZVRN.cjs +152 -0
  9. package/dist/chunk-CNSGZVRN.cjs.map +1 -0
  10. package/dist/chunk-JF5AJQMU.cjs +2788 -0
  11. package/dist/chunk-JF5AJQMU.cjs.map +1 -0
  12. package/dist/chunk-NDSL7NPN.js +807 -0
  13. package/dist/chunk-NDSL7NPN.js.map +1 -0
  14. package/dist/chunk-OHXW2YM6.js +2708 -0
  15. package/dist/chunk-OHXW2YM6.js.map +1 -0
  16. package/dist/chunk-Q3SOMX26.js +2854 -0
  17. package/dist/chunk-Q3SOMX26.js.map +1 -0
  18. package/dist/chunk-WH3APNQ5.js +147 -0
  19. package/dist/chunk-WH3APNQ5.js.map +1 -0
  20. package/dist/chunk-X35MHWXE.cjs +817 -0
  21. package/dist/chunk-X35MHWXE.cjs.map +1 -0
  22. package/dist/cli/index.cjs +926 -0
  23. package/dist/cli/index.cjs.map +1 -0
  24. package/dist/cli/index.d.cts +24 -0
  25. package/dist/cli/index.d.ts +24 -0
  26. package/dist/cli/index.js +916 -0
  27. package/dist/cli/index.js.map +1 -0
  28. package/dist/index-DPsZ1zat.d.ts +447 -0
  29. package/dist/index-RTPmFjMp.d.cts +447 -0
  30. package/dist/index.cjs +508 -0
  31. package/dist/index.cjs.map +1 -0
  32. package/dist/index.d.cts +664 -0
  33. package/dist/index.d.ts +664 -0
  34. package/dist/index.js +204 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/models/index.cjs +62 -0
  37. package/dist/models/index.cjs.map +1 -0
  38. package/dist/models/index.d.cts +165 -0
  39. package/dist/models/index.d.ts +165 -0
  40. package/dist/models/index.js +5 -0
  41. package/dist/models/index.js.map +1 -0
  42. package/dist/tools/index.cjs +207 -0
  43. package/dist/tools/index.cjs.map +1 -0
  44. package/dist/tools/index.d.cts +108 -0
  45. package/dist/tools/index.d.ts +108 -0
  46. package/dist/tools/index.js +6 -0
  47. package/dist/tools/index.js.map +1 -0
  48. package/dist/types-C0aX_Qdp.d.cts +917 -0
  49. package/dist/types-C0aX_Qdp.d.ts +917 -0
  50. package/package.json +80 -0
package/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ #!/usr/bin/env node
2
+ import './chunk-A3S3AGE3.js';
3
+ export { Agent, DEFAULT_SYSTEM_PROMPT, JsonlStorage, MCPAdapter, MCPClient, MemoryManager, MemoryStorage, SessionManager, SkillLoader, SkillRegistry, StreamChunkProcessor, createAgent, createJsonlStorage, createMCPAdapter, createMCPClient, createMemoryStorage, createSessionManager, createSkillLoader, createSkillRegistry, createStorage, getLatestSessionId, getSessionStoragePath, loadMCPConfig, parseSkillMd, validateMCPConfig } from './chunk-Q3SOMX26.js';
4
+ export { AnthropicAdapter, OllamaAdapter, OpenAIAdapter, createAnthropic, createModel, createOllama, createOpenAI } from './chunk-NDSL7NPN.js';
5
+ export { DEFAULT_GREP_HEAD_LIMIT, HookManager, MAX_LINE_LENGTH, ToolRegistry, agentTool, bashTool, buildHookEnv, createAgentTool, createAskUserQuestionTool, createFunctionHook, createSkillTool, createTool, editTool, formatAnswerSummary, formatAskUserQuestionPrompt, getAllBuiltinTools, getFileSystemTools, getGlobalRegistry, getGrepTools, getInteractionTools, getSafeBuiltinTools, getShellTools, getSkillTools, getSubagentTools, getTaskTools, getWebTools, globTool, grepTool, loadHooksSettingsFromProject, loadHooksSettingsFromUser, matchTool, mergeCommandHookLayers, parseHooksSettingsFile, questionTool, readFileTool, subagentRequestSchema, taskCreateTool, taskListTool, taskUpdateTool, truncateMatchLineForDisplay, webFetchTool, webSearchTool, writeFileTool } from './chunk-OHXW2YM6.js';
6
+ import './chunk-WH3APNQ5.js';
7
+
8
+ // src/streaming/event-emitter.ts
9
+ var AgentStream = class _AgentStream {
10
+ events = [];
11
+ resolvers = [];
12
+ isEnded = false;
13
+ error = null;
14
+ abortController;
15
+ constructor() {
16
+ this.abortController = new AbortController();
17
+ }
18
+ /**
19
+ * 实现 AsyncIterable 接口
20
+ */
21
+ [Symbol.asyncIterator]() {
22
+ return {
23
+ next: async () => {
24
+ if (this.abortController.signal.aborted) {
25
+ return { done: true, value: void 0 };
26
+ }
27
+ if (this.error) {
28
+ throw this.error;
29
+ }
30
+ if (this.events.length > 0) {
31
+ const event = this.events.shift();
32
+ return { done: false, value: event };
33
+ }
34
+ if (this.isEnded) {
35
+ return { done: true, value: void 0 };
36
+ }
37
+ return new Promise((resolve, reject) => {
38
+ this.resolvers.push({ resolve, reject });
39
+ });
40
+ }
41
+ };
42
+ }
43
+ /**
44
+ * 推送事件
45
+ */
46
+ push(event) {
47
+ if (this.error) return;
48
+ if (this.resolvers.length > 0) {
49
+ const resolver = this.resolvers.shift();
50
+ resolver.resolve({ done: false, value: event });
51
+ } else {
52
+ this.events.push(event);
53
+ }
54
+ }
55
+ /**
56
+ * 结束流
57
+ */
58
+ end(usage) {
59
+ if (usage) {
60
+ this.push({ type: "model_usage", usage });
61
+ }
62
+ this.push({ type: "end", timestamp: Date.now() });
63
+ this.finalize();
64
+ }
65
+ /**
66
+ * Marks the stream complete without pushing an event. Use when the producer already emitted a terminal `{ type: 'end' }` and iterators must unblock.
67
+ */
68
+ finalize() {
69
+ if (this.isEnded) {
70
+ return;
71
+ }
72
+ this.isEnded = true;
73
+ while (this.resolvers.length > 0) {
74
+ const resolver = this.resolvers.shift();
75
+ resolver.resolve({ done: true, value: void 0 });
76
+ }
77
+ }
78
+ /**
79
+ * 抛出错误
80
+ */
81
+ throwError(error) {
82
+ this.error = error;
83
+ while (this.resolvers.length > 0) {
84
+ const resolver = this.resolvers.shift();
85
+ resolver.reject(error);
86
+ }
87
+ }
88
+ /**
89
+ * 中止流
90
+ */
91
+ abort() {
92
+ this.abortController.abort();
93
+ this.isEnded = true;
94
+ while (this.resolvers.length > 0) {
95
+ const resolver = this.resolvers.shift();
96
+ resolver.resolve({ done: true, value: void 0 });
97
+ }
98
+ }
99
+ /**
100
+ * 获取中止信号
101
+ */
102
+ get signal() {
103
+ return this.abortController.signal;
104
+ }
105
+ /**
106
+ * 合并多个流
107
+ */
108
+ static merge(...streams) {
109
+ const merged = new _AgentStream();
110
+ let completedCount = 0;
111
+ for (const stream of streams) {
112
+ (async () => {
113
+ try {
114
+ for await (const event of stream) {
115
+ merged.push(event);
116
+ }
117
+ } catch (error) {
118
+ merged.throwError(error);
119
+ } finally {
120
+ completedCount++;
121
+ if (completedCount === streams.length) {
122
+ merged.end();
123
+ }
124
+ }
125
+ })();
126
+ }
127
+ return merged;
128
+ }
129
+ /**
130
+ * 转换为数组
131
+ */
132
+ async toArray() {
133
+ const events = [];
134
+ for await (const event of this) {
135
+ events.push(event);
136
+ }
137
+ return events;
138
+ }
139
+ /**
140
+ * 收集文本内容
141
+ */
142
+ async collectText() {
143
+ let text = "";
144
+ for await (const event of this) {
145
+ if (event.type === "text_delta") {
146
+ text += event.content;
147
+ }
148
+ }
149
+ return text;
150
+ }
151
+ /**
152
+ * 过滤事件
153
+ */
154
+ filter(predicate) {
155
+ const filtered = new _AgentStream();
156
+ (async () => {
157
+ try {
158
+ for await (const event of this) {
159
+ if (predicate(event)) {
160
+ filtered.push(event);
161
+ }
162
+ }
163
+ filtered.end();
164
+ } catch (error) {
165
+ filtered.throwError(error);
166
+ }
167
+ })();
168
+ return filtered;
169
+ }
170
+ /**
171
+ * 转换事件
172
+ */
173
+ map(transform) {
174
+ const self = this;
175
+ return {
176
+ async *[Symbol.asyncIterator]() {
177
+ for await (const event of self) {
178
+ yield transform(event);
179
+ }
180
+ }
181
+ };
182
+ }
183
+ };
184
+ function createStream() {
185
+ return new AgentStream();
186
+ }
187
+ function fromAsyncIterable(iterable) {
188
+ const stream = new AgentStream();
189
+ (async () => {
190
+ try {
191
+ for await (const event of iterable) {
192
+ stream.push(event);
193
+ }
194
+ stream.end();
195
+ } catch (error) {
196
+ stream.throwError(error);
197
+ }
198
+ })();
199
+ return stream;
200
+ }
201
+
202
+ export { AgentStream, createStream, fromAsyncIterable };
203
+ //# sourceMappingURL=index.js.map
204
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/streaming/event-emitter.ts"],"names":[],"mappings":";;;;;;;;AAMO,IAAM,WAAA,GAAN,MAAM,YAAA,CAAkD;AAAA,EACrD,SAAwB,EAAC;AAAA,EACzB,YAGH,EAAC;AAAA,EACE,OAAA,GAAU,KAAA;AAAA,EACV,KAAA,GAAsB,IAAA;AAAA,EACtB,eAAA;AAAA,EAER,WAAA,GAAc;AACZ,IAAA,IAAA,CAAK,eAAA,GAAkB,IAAI,eAAA,EAAgB;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA,EAKA,CAAC,MAAA,CAAO,aAAa,CAAA,GAAgC;AACnD,IAAA,OAAO;AAAA,MACL,MAAM,YAAkD;AAEtD,QAAA,IAAI,IAAA,CAAK,eAAA,CAAgB,MAAA,CAAO,OAAA,EAAS;AACvC,UAAA,OAAO,EAAE,IAAA,EAAM,IAAA,EAAM,KAAA,EAAO,MAAA,EAAU;AAAA,QACxC;AAGA,QAAA,IAAI,KAAK,KAAA,EAAO;AACd,UAAA,MAAM,IAAA,CAAK,KAAA;AAAA,QACb;AAGA,QAAA,IAAI,IAAA,CAAK,MAAA,CAAO,MAAA,GAAS,CAAA,EAAG;AAC1B,UAAA,MAAM,KAAA,GAAQ,IAAA,CAAK,MAAA,CAAO,KAAA,EAAM;AAChC,UAAA,OAAO,EAAE,IAAA,EAAM,KAAA,EAAO,KAAA,EAAO,KAAA,EAAM;AAAA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A,CAAS,WAAW,KAAc,CAAA;AAAA,MACpC;AAAA,IACF,CAAA,GAAG;AAEH,IAAA,OAAO,QAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,IAAO,SAAA,EAAwD;AAC7D,IAAA,MAAM,IAAA,GAAO,IAAA;AAEb,IAAA,OAAO;AAAA,MACL,QAAQ,MAAA,CAAO,aAAa,CAAA,GAAI;AAC9B,QAAA,WAAA,MAAiB,SAAS,IAAA,EAAM;AAC9B,UAAA,MAAM,UAAU,KAAK,CAAA;AAAA,QACvB;AAAA,MACF;AAAA,KACF;AAAA,EACF;AACF;AAKO,SAAS,YAAA,GAA4B;AAC1C,EAAA,OAAO,IAAI,WAAA,EAAY;AACzB;AAKO,SAAS,kBAAkB,QAAA,EAAmD;AACnF,EAAA,MAAM,MAAA,GAAS,IAAI,WAAA,EAAY;AAE/B,EAAA,CAAC,YAAY;AACX,IAAA,IAAI;AACF,MAAA,WAAA,MAAiB,SAAS,QAAA,EAAU;AAClC,QAAA,MAAA,CAAO,KAAK,KAAK,CAAA;AAAA,MACnB;AACA,MAAA,MAAA,CAAO,GAAA,EAAI;AAAA,IACb,SAAS,KAAA,EAAO;AACd,MAAA,MAAA,CAAO,WAAW,KAAc,CAAA;AAAA,IAClC;AAAA,EACF,CAAA,GAAG;AAEH,EAAA,OAAO,MAAA;AACT","file":"index.js","sourcesContent":["import type { StreamEvent, TokenUsage } from '../core/types.js';\n\n/**\n * Agent 流类\n * 实现 AsyncIterable 接口,支持 for await...of 遍历\n */\nexport class AgentStream implements AsyncIterable<StreamEvent> {\n private events: StreamEvent[] = [];\n private resolvers: Array<{\n resolve: (value: IteratorResult<StreamEvent>) => void;\n reject: (error: Error) => void;\n }> = [];\n private isEnded = false;\n private error: Error | null = null;\n private abortController: AbortController;\n\n constructor() {\n this.abortController = new AbortController();\n }\n\n /**\n * 实现 AsyncIterable 接口\n */\n [Symbol.asyncIterator](): AsyncIterator<StreamEvent> {\n return {\n next: async (): Promise<IteratorResult<StreamEvent>> => {\n // 检查是否中止\n if (this.abortController.signal.aborted) {\n return { done: true, value: undefined };\n }\n\n // 检查是否有错误\n if (this.error) {\n throw this.error;\n }\n\n // 检查是否有缓存事件\n if (this.events.length > 0) {\n const event = this.events.shift()!;\n return { done: false, value: event };\n }\n\n // 检查是否已结束\n if (this.isEnded) {\n return { done: true, value: undefined };\n }\n\n // 等待新事件\n return new Promise((resolve, reject) => {\n this.resolvers.push({ resolve, reject });\n });\n }\n };\n }\n\n /**\n * 推送事件\n */\n push(event: StreamEvent): void {\n if (this.error) return;\n\n // 如果有等待的 resolver,直接 resolve\n if (this.resolvers.length > 0) {\n const resolver = this.resolvers.shift()!;\n resolver.resolve({ done: false, value: event });\n } else {\n // 否则缓存事件\n this.events.push(event);\n }\n }\n\n /**\n * 结束流\n */\n end(usage?: TokenUsage): void {\n if (usage) {\n this.push({ type: 'model_usage', usage });\n }\n\n this.push({ type: 'end', timestamp: Date.now() });\n\n this.finalize();\n }\n\n /**\n * Marks the stream complete without pushing an event. Use when the producer already emitted a terminal `{ type: 'end' }` and iterators must unblock.\n */\n finalize(): void {\n if (this.isEnded) {\n return;\n }\n this.isEnded = true;\n\n while (this.resolvers.length > 0) {\n const resolver = this.resolvers.shift()!;\n resolver.resolve({ done: true, value: undefined });\n }\n }\n\n /**\n * 抛出错误\n */\n throwError(error: Error): void {\n this.error = error;\n\n // reject 所有等待的 resolver\n while (this.resolvers.length > 0) {\n const resolver = this.resolvers.shift()!;\n resolver.reject(error);\n }\n }\n\n /**\n * 中止流\n */\n abort(): void {\n this.abortController.abort();\n this.isEnded = true;\n\n // resolve 所有等待的 resolver\n while (this.resolvers.length > 0) {\n const resolver = this.resolvers.shift()!;\n resolver.resolve({ done: true, value: undefined });\n }\n }\n\n /**\n * 获取中止信号\n */\n get signal(): AbortSignal {\n return this.abortController.signal;\n }\n\n /**\n * 合并多个流\n */\n static merge(...streams: AgentStream[]): AgentStream {\n const merged = new AgentStream();\n let completedCount = 0;\n\n for (const stream of streams) {\n (async () => {\n try {\n for await (const event of stream) {\n merged.push(event);\n }\n } catch (error) {\n merged.throwError(error as Error);\n } finally {\n completedCount++;\n if (completedCount === streams.length) {\n merged.end();\n }\n }\n })();\n }\n\n return merged;\n }\n\n /**\n * 转换为数组\n */\n async toArray(): Promise<StreamEvent[]> {\n const events: StreamEvent[] = [];\n for await (const event of this) {\n events.push(event);\n }\n return events;\n }\n\n /**\n * 收集文本内容\n */\n async collectText(): Promise<string> {\n let text = '';\n for await (const event of this) {\n if (event.type === 'text_delta') {\n text += event.content;\n }\n }\n return text;\n }\n\n /**\n * 过滤事件\n */\n filter(predicate: (event: StreamEvent) => boolean): AgentStream {\n const filtered = new AgentStream();\n\n (async () => {\n try {\n for await (const event of this) {\n if (predicate(event)) {\n filtered.push(event);\n }\n }\n filtered.end();\n } catch (error) {\n filtered.throwError(error as Error);\n }\n })();\n\n return filtered;\n }\n\n /**\n * 转换事件\n */\n map<T>(transform: (event: StreamEvent) => T): AsyncIterable<T> {\n const self = this;\n\n return {\n async *[Symbol.asyncIterator]() {\n for await (const event of self) {\n yield transform(event);\n }\n }\n };\n }\n}\n\n/**\n * 创建 Agent 流\n */\nexport function createStream(): AgentStream {\n return new AgentStream();\n}\n\n/**\n * 从 AsyncIterable 创建 Agent 流\n */\nexport function fromAsyncIterable(iterable: AsyncIterable<StreamEvent>): AgentStream {\n const stream = new AgentStream();\n\n (async () => {\n try {\n for await (const event of iterable) {\n stream.push(event);\n }\n stream.end();\n } catch (error) {\n stream.throwError(error as Error);\n }\n })();\n\n return stream;\n}\n"]}
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ var chunkX35MHWXE_cjs = require('../chunk-X35MHWXE.cjs');
5
+ var chunkCNSGZVRN_cjs = require('../chunk-CNSGZVRN.cjs');
6
+
7
+
8
+
9
+ Object.defineProperty(exports, "AnthropicAdapter", {
10
+ enumerable: true,
11
+ get: function () { return chunkX35MHWXE_cjs.AnthropicAdapter; }
12
+ });
13
+ Object.defineProperty(exports, "OllamaAdapter", {
14
+ enumerable: true,
15
+ get: function () { return chunkX35MHWXE_cjs.OllamaAdapter; }
16
+ });
17
+ Object.defineProperty(exports, "OpenAIAdapter", {
18
+ enumerable: true,
19
+ get: function () { return chunkX35MHWXE_cjs.OpenAIAdapter; }
20
+ });
21
+ Object.defineProperty(exports, "createAnthropic", {
22
+ enumerable: true,
23
+ get: function () { return chunkX35MHWXE_cjs.createAnthropic; }
24
+ });
25
+ Object.defineProperty(exports, "createModel", {
26
+ enumerable: true,
27
+ get: function () { return chunkX35MHWXE_cjs.createModel; }
28
+ });
29
+ Object.defineProperty(exports, "createOllama", {
30
+ enumerable: true,
31
+ get: function () { return chunkX35MHWXE_cjs.createOllama; }
32
+ });
33
+ Object.defineProperty(exports, "createOpenAI", {
34
+ enumerable: true,
35
+ get: function () { return chunkX35MHWXE_cjs.createOpenAI; }
36
+ });
37
+ Object.defineProperty(exports, "ollamaMessageContentToApiString", {
38
+ enumerable: true,
39
+ get: function () { return chunkX35MHWXE_cjs.ollamaMessageContentToApiString; }
40
+ });
41
+ Object.defineProperty(exports, "ollamaStreamChunksFromChatData", {
42
+ enumerable: true,
43
+ get: function () { return chunkX35MHWXE_cjs.ollamaStreamChunksFromChatData; }
44
+ });
45
+ Object.defineProperty(exports, "BaseModelAdapter", {
46
+ enumerable: true,
47
+ get: function () { return chunkCNSGZVRN_cjs.BaseModelAdapter; }
48
+ });
49
+ Object.defineProperty(exports, "mergeTokenUsage", {
50
+ enumerable: true,
51
+ get: function () { return chunkCNSGZVRN_cjs.mergeTokenUsage; }
52
+ });
53
+ Object.defineProperty(exports, "toolsToModelSchema", {
54
+ enumerable: true,
55
+ get: function () { return chunkCNSGZVRN_cjs.toolsToModelSchema; }
56
+ });
57
+ Object.defineProperty(exports, "zodToJsonSchema", {
58
+ enumerable: true,
59
+ get: function () { return chunkCNSGZVRN_cjs.zodToJsonSchema; }
60
+ });
61
+ //# sourceMappingURL=index.cjs.map
62
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
@@ -0,0 +1,165 @@
1
+ import { z } from 'zod';
2
+ import { O as ModelAdapter, Q as ModelCapabilities, R as ModelParams, j as StreamChunk, t as CompletionResult, i as TokenUsage, c as ToolDefinition, ac as ToolSchema, u as ContentPart } from '../types-C0aX_Qdp.cjs';
3
+
4
+ /**
5
+ * 将 Zod Schema 转换为 JSON Schema
6
+ */
7
+ declare function zodToJsonSchema(schema: z.ZodSchema): unknown;
8
+ /**
9
+ * 将工具定义转换为模型工具 Schema
10
+ */
11
+ declare function toolsToModelSchema(tools: ToolDefinition[]): ToolSchema[];
12
+ /**
13
+ * 合并 Token 使用统计
14
+ */
15
+ declare function mergeTokenUsage(...usages: (TokenUsage | undefined)[]): TokenUsage;
16
+ /**
17
+ * 基础模型适配器抽象类
18
+ */
19
+ declare abstract class BaseModelAdapter implements ModelAdapter {
20
+ abstract readonly name: string;
21
+ /** 模型能力描述 */
22
+ capabilities?: ModelCapabilities;
23
+ abstract stream(params: ModelParams): AsyncIterable<StreamChunk>;
24
+ abstract complete(params: ModelParams): Promise<CompletionResult>;
25
+ /**
26
+ * 转换消息格式
27
+ */
28
+ protected transformMessages(messages: ModelParams['messages']): unknown[];
29
+ }
30
+
31
+ /**
32
+ * OpenAI 配置
33
+ */
34
+ interface OpenAIConfig {
35
+ apiKey?: string;
36
+ baseUrl?: string;
37
+ model?: string;
38
+ organization?: string;
39
+ /** 自定义模型能力 (覆盖默认值) */
40
+ capabilities?: ModelCapabilities;
41
+ }
42
+ /**
43
+ * OpenAI 模型适配器
44
+ */
45
+ declare class OpenAIAdapter extends BaseModelAdapter {
46
+ readonly name: string;
47
+ private apiKey;
48
+ private baseUrl;
49
+ private model;
50
+ private organization?;
51
+ constructor(config?: OpenAIConfig);
52
+ stream(params: ModelParams): AsyncIterable<StreamChunk>;
53
+ complete(params: ModelParams): Promise<CompletionResult>;
54
+ private buildRequestBody;
55
+ private fetch;
56
+ private safeParseJSON;
57
+ }
58
+ /**
59
+ * 创建 OpenAI 适配器
60
+ */
61
+ declare function createOpenAI(config?: OpenAIConfig): OpenAIAdapter;
62
+
63
+ /**
64
+ * Anthropic 配置
65
+ */
66
+ interface AnthropicConfig {
67
+ apiKey?: string;
68
+ baseUrl?: string;
69
+ model?: string;
70
+ version?: string;
71
+ /** 自定义模型能力 (覆盖默认值) */
72
+ capabilities?: ModelCapabilities;
73
+ }
74
+ /**
75
+ * Anthropic 模型适配器
76
+ */
77
+ declare class AnthropicAdapter extends BaseModelAdapter {
78
+ readonly name: string;
79
+ private apiKey;
80
+ private baseUrl;
81
+ private model;
82
+ private version;
83
+ constructor(config?: AnthropicConfig);
84
+ stream(params: ModelParams): AsyncIterable<StreamChunk>;
85
+ complete(params: ModelParams): Promise<CompletionResult>;
86
+ private buildRequestBody;
87
+ private extractSystemMessage;
88
+ private transformAnthropicMessages;
89
+ private fetch;
90
+ private safeParseJSON;
91
+ }
92
+ /**
93
+ * 创建 Anthropic 适配器
94
+ */
95
+ declare function createAnthropic(config?: AnthropicConfig): AnthropicAdapter;
96
+
97
+ /**
98
+ * Ollama `/api/chat` `think` parameter (see https://docs.ollama.com/capabilities/thinking).
99
+ */
100
+ type OllamaThinkOption = boolean | 'low' | 'medium' | 'high';
101
+ /**
102
+ * Ollama 配置
103
+ */
104
+ interface OllamaConfig {
105
+ baseUrl?: string;
106
+ model?: string;
107
+ /** 自定义模型能力 (覆盖默认值) */
108
+ capabilities?: ModelCapabilities;
109
+ /**
110
+ * When set, sent as top-level `think` on `/api/chat`.
111
+ * Omit to use the server default for the model.
112
+ */
113
+ think?: OllamaThinkOption;
114
+ }
115
+ /**
116
+ * Map one Ollama `/api/chat` stream JSON object to stream chunks (thinking before text).
117
+ * @internal Exported for unit tests.
118
+ */
119
+ declare function ollamaStreamChunksFromChatData(data: Record<string, unknown>, parseToolArguments: (args: unknown) => Record<string, unknown>, nextToolCallId: () => string): StreamChunk[];
120
+ /**
121
+ * Ollama `/api/chat` requires string `content` on each message. The Agent may persist assistant
122
+ * turns as `ContentPart[]` (e.g. thinking + text); replay only `text` parts so the JSON matches
123
+ * Ollama's schema (thinking is not re-sent; the model produces a new trace each request).
124
+ */
125
+ declare function ollamaMessageContentToApiString(content: string | ContentPart[]): string;
126
+ /**
127
+ * Ollama 模型适配器 (本地模型)
128
+ */
129
+ declare class OllamaAdapter extends BaseModelAdapter {
130
+ readonly name: string;
131
+ private baseUrl;
132
+ private model;
133
+ private readonly think;
134
+ constructor(config?: OllamaConfig);
135
+ stream(params: ModelParams): AsyncIterable<StreamChunk>;
136
+ complete(params: ModelParams): Promise<CompletionResult>;
137
+ private parseToolArguments;
138
+ /**
139
+ * Ollama 要求 tool_calls.function.arguments 为对象,而非 JSON 字符串。
140
+ * 工具结果消息使用 tool_name(见 https://docs.ollama.com/capabilities/tool-calling ),非 OpenAI 的 tool_call_id。
141
+ */
142
+ protected transformMessages(messages: ModelParams['messages']): unknown[];
143
+ private buildRequestBody;
144
+ private fetch;
145
+ }
146
+ /**
147
+ * 创建 Ollama 模型适配器
148
+ */
149
+ declare function createOllama(config?: OllamaConfig): OllamaAdapter;
150
+
151
+ type ModelProvider = 'openai' | 'anthropic' | 'ollama';
152
+ interface CreateModelConfig {
153
+ provider: ModelProvider;
154
+ apiKey?: string;
155
+ baseUrl?: string;
156
+ model?: string;
157
+ /** Ollama only: passed as `think` on `/api/chat`. */
158
+ think?: OllamaThinkOption;
159
+ }
160
+ /**
161
+ * 创建模型适配器工厂函数
162
+ */
163
+ declare function createModel(config: CreateModelConfig): ModelAdapter;
164
+
165
+ export { AnthropicAdapter, type AnthropicConfig, BaseModelAdapter, type CreateModelConfig, type ModelProvider, OllamaAdapter, type OllamaConfig, type OllamaThinkOption, OpenAIAdapter, type OpenAIConfig, createAnthropic, createModel, createOllama, createOpenAI, mergeTokenUsage, ollamaMessageContentToApiString, ollamaStreamChunksFromChatData, toolsToModelSchema, zodToJsonSchema };
@@ -0,0 +1,165 @@
1
+ import { z } from 'zod';
2
+ import { O as ModelAdapter, Q as ModelCapabilities, R as ModelParams, j as StreamChunk, t as CompletionResult, i as TokenUsage, c as ToolDefinition, ac as ToolSchema, u as ContentPart } from '../types-C0aX_Qdp.js';
3
+
4
+ /**
5
+ * 将 Zod Schema 转换为 JSON Schema
6
+ */
7
+ declare function zodToJsonSchema(schema: z.ZodSchema): unknown;
8
+ /**
9
+ * 将工具定义转换为模型工具 Schema
10
+ */
11
+ declare function toolsToModelSchema(tools: ToolDefinition[]): ToolSchema[];
12
+ /**
13
+ * 合并 Token 使用统计
14
+ */
15
+ declare function mergeTokenUsage(...usages: (TokenUsage | undefined)[]): TokenUsage;
16
+ /**
17
+ * 基础模型适配器抽象类
18
+ */
19
+ declare abstract class BaseModelAdapter implements ModelAdapter {
20
+ abstract readonly name: string;
21
+ /** 模型能力描述 */
22
+ capabilities?: ModelCapabilities;
23
+ abstract stream(params: ModelParams): AsyncIterable<StreamChunk>;
24
+ abstract complete(params: ModelParams): Promise<CompletionResult>;
25
+ /**
26
+ * 转换消息格式
27
+ */
28
+ protected transformMessages(messages: ModelParams['messages']): unknown[];
29
+ }
30
+
31
+ /**
32
+ * OpenAI 配置
33
+ */
34
+ interface OpenAIConfig {
35
+ apiKey?: string;
36
+ baseUrl?: string;
37
+ model?: string;
38
+ organization?: string;
39
+ /** 自定义模型能力 (覆盖默认值) */
40
+ capabilities?: ModelCapabilities;
41
+ }
42
+ /**
43
+ * OpenAI 模型适配器
44
+ */
45
+ declare class OpenAIAdapter extends BaseModelAdapter {
46
+ readonly name: string;
47
+ private apiKey;
48
+ private baseUrl;
49
+ private model;
50
+ private organization?;
51
+ constructor(config?: OpenAIConfig);
52
+ stream(params: ModelParams): AsyncIterable<StreamChunk>;
53
+ complete(params: ModelParams): Promise<CompletionResult>;
54
+ private buildRequestBody;
55
+ private fetch;
56
+ private safeParseJSON;
57
+ }
58
+ /**
59
+ * 创建 OpenAI 适配器
60
+ */
61
+ declare function createOpenAI(config?: OpenAIConfig): OpenAIAdapter;
62
+
63
+ /**
64
+ * Anthropic 配置
65
+ */
66
+ interface AnthropicConfig {
67
+ apiKey?: string;
68
+ baseUrl?: string;
69
+ model?: string;
70
+ version?: string;
71
+ /** 自定义模型能力 (覆盖默认值) */
72
+ capabilities?: ModelCapabilities;
73
+ }
74
+ /**
75
+ * Anthropic 模型适配器
76
+ */
77
+ declare class AnthropicAdapter extends BaseModelAdapter {
78
+ readonly name: string;
79
+ private apiKey;
80
+ private baseUrl;
81
+ private model;
82
+ private version;
83
+ constructor(config?: AnthropicConfig);
84
+ stream(params: ModelParams): AsyncIterable<StreamChunk>;
85
+ complete(params: ModelParams): Promise<CompletionResult>;
86
+ private buildRequestBody;
87
+ private extractSystemMessage;
88
+ private transformAnthropicMessages;
89
+ private fetch;
90
+ private safeParseJSON;
91
+ }
92
+ /**
93
+ * 创建 Anthropic 适配器
94
+ */
95
+ declare function createAnthropic(config?: AnthropicConfig): AnthropicAdapter;
96
+
97
+ /**
98
+ * Ollama `/api/chat` `think` parameter (see https://docs.ollama.com/capabilities/thinking).
99
+ */
100
+ type OllamaThinkOption = boolean | 'low' | 'medium' | 'high';
101
+ /**
102
+ * Ollama 配置
103
+ */
104
+ interface OllamaConfig {
105
+ baseUrl?: string;
106
+ model?: string;
107
+ /** 自定义模型能力 (覆盖默认值) */
108
+ capabilities?: ModelCapabilities;
109
+ /**
110
+ * When set, sent as top-level `think` on `/api/chat`.
111
+ * Omit to use the server default for the model.
112
+ */
113
+ think?: OllamaThinkOption;
114
+ }
115
+ /**
116
+ * Map one Ollama `/api/chat` stream JSON object to stream chunks (thinking before text).
117
+ * @internal Exported for unit tests.
118
+ */
119
+ declare function ollamaStreamChunksFromChatData(data: Record<string, unknown>, parseToolArguments: (args: unknown) => Record<string, unknown>, nextToolCallId: () => string): StreamChunk[];
120
+ /**
121
+ * Ollama `/api/chat` requires string `content` on each message. The Agent may persist assistant
122
+ * turns as `ContentPart[]` (e.g. thinking + text); replay only `text` parts so the JSON matches
123
+ * Ollama's schema (thinking is not re-sent; the model produces a new trace each request).
124
+ */
125
+ declare function ollamaMessageContentToApiString(content: string | ContentPart[]): string;
126
+ /**
127
+ * Ollama 模型适配器 (本地模型)
128
+ */
129
+ declare class OllamaAdapter extends BaseModelAdapter {
130
+ readonly name: string;
131
+ private baseUrl;
132
+ private model;
133
+ private readonly think;
134
+ constructor(config?: OllamaConfig);
135
+ stream(params: ModelParams): AsyncIterable<StreamChunk>;
136
+ complete(params: ModelParams): Promise<CompletionResult>;
137
+ private parseToolArguments;
138
+ /**
139
+ * Ollama 要求 tool_calls.function.arguments 为对象,而非 JSON 字符串。
140
+ * 工具结果消息使用 tool_name(见 https://docs.ollama.com/capabilities/tool-calling ),非 OpenAI 的 tool_call_id。
141
+ */
142
+ protected transformMessages(messages: ModelParams['messages']): unknown[];
143
+ private buildRequestBody;
144
+ private fetch;
145
+ }
146
+ /**
147
+ * 创建 Ollama 模型适配器
148
+ */
149
+ declare function createOllama(config?: OllamaConfig): OllamaAdapter;
150
+
151
+ type ModelProvider = 'openai' | 'anthropic' | 'ollama';
152
+ interface CreateModelConfig {
153
+ provider: ModelProvider;
154
+ apiKey?: string;
155
+ baseUrl?: string;
156
+ model?: string;
157
+ /** Ollama only: passed as `think` on `/api/chat`. */
158
+ think?: OllamaThinkOption;
159
+ }
160
+ /**
161
+ * 创建模型适配器工厂函数
162
+ */
163
+ declare function createModel(config: CreateModelConfig): ModelAdapter;
164
+
165
+ export { AnthropicAdapter, type AnthropicConfig, BaseModelAdapter, type CreateModelConfig, type ModelProvider, OllamaAdapter, type OllamaConfig, type OllamaThinkOption, OpenAIAdapter, type OpenAIConfig, createAnthropic, createModel, createOllama, createOpenAI, mergeTokenUsage, ollamaMessageContentToApiString, ollamaStreamChunksFromChatData, toolsToModelSchema, zodToJsonSchema };
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ export { AnthropicAdapter, OllamaAdapter, OpenAIAdapter, createAnthropic, createModel, createOllama, createOpenAI, ollamaMessageContentToApiString, ollamaStreamChunksFromChatData } from '../chunk-NDSL7NPN.js';
3
+ export { BaseModelAdapter, mergeTokenUsage, toolsToModelSchema, zodToJsonSchema } from '../chunk-WH3APNQ5.js';
4
+ //# sourceMappingURL=index.js.map
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}