@codeany/open-agent-sdk 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +113 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +446 -0
- package/dist/agent.js.map +1 -0
- package/dist/engine.d.ts +62 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +494 -0
- package/dist/engine.js.map +1 -0
- package/dist/hooks.d.ts +111 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +179 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +19 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +126 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/providers/anthropic.d.ts +17 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +47 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/index.d.ts +20 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +26 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai.d.ts +26 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +212 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/types.d.ts +84 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +11 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/sdk-mcp-server.d.ts +52 -0
- package/dist/sdk-mcp-server.d.ts.map +1 -0
- package/dist/sdk-mcp-server.js +57 -0
- package/dist/sdk-mcp-server.js.map +1 -0
- package/dist/session.d.ts +73 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +159 -0
- package/dist/session.js.map +1 -0
- package/dist/skills/bundled/commit.d.ts +7 -0
- package/dist/skills/bundled/commit.d.ts.map +1 -0
- package/dist/skills/bundled/commit.js +35 -0
- package/dist/skills/bundled/commit.js.map +1 -0
- package/dist/skills/bundled/debug.d.ts +7 -0
- package/dist/skills/bundled/debug.d.ts.map +1 -0
- package/dist/skills/bundled/debug.js +46 -0
- package/dist/skills/bundled/debug.js.map +1 -0
- package/dist/skills/bundled/index.d.ts +11 -0
- package/dist/skills/bundled/index.d.ts.map +1 -0
- package/dist/skills/bundled/index.js +26 -0
- package/dist/skills/bundled/index.js.map +1 -0
- package/dist/skills/bundled/review.d.ts +7 -0
- package/dist/skills/bundled/review.d.ts.map +1 -0
- package/dist/skills/bundled/review.js +38 -0
- package/dist/skills/bundled/review.js.map +1 -0
- package/dist/skills/bundled/simplify.d.ts +8 -0
- package/dist/skills/bundled/simplify.d.ts.map +1 -0
- package/dist/skills/bundled/simplify.js +48 -0
- package/dist/skills/bundled/simplify.js.map +1 -0
- package/dist/skills/bundled/test.d.ts +7 -0
- package/dist/skills/bundled/test.d.ts.map +1 -0
- package/dist/skills/bundled/test.js +40 -0
- package/dist/skills/bundled/test.js.map +1 -0
- package/dist/skills/index.d.ts +7 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +8 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/skills/registry.d.ts +43 -0
- package/dist/skills/registry.d.ts.map +1 -0
- package/dist/skills/registry.js +111 -0
- package/dist/skills/registry.js.map +1 -0
- package/dist/skills/types.d.ts +83 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +8 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/tool-helper.d.ts +73 -0
- package/dist/tool-helper.d.ts.map +1 -0
- package/dist/tool-helper.js +86 -0
- package/dist/tool-helper.js.map +1 -0
- package/dist/tools/agent-tool.d.ts +17 -0
- package/dist/tools/agent-tool.d.ts.map +1 -0
- package/dist/tools/agent-tool.js +146 -0
- package/dist/tools/agent-tool.js.map +1 -0
- package/dist/tools/ask-user.d.ts +18 -0
- package/dist/tools/ask-user.d.ts.map +1 -0
- package/dist/tools/ask-user.js +72 -0
- package/dist/tools/ask-user.js.map +1 -0
- package/dist/tools/bash.d.ts +5 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +67 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/config-tool.d.ts +20 -0
- package/dist/tools/config-tool.d.ts.map +1 -0
- package/dist/tools/config-tool.js +83 -0
- package/dist/tools/config-tool.js.map +1 -0
- package/dist/tools/cron-tools.d.ts +33 -0
- package/dist/tools/cron-tools.d.ts.map +1 -0
- package/dist/tools/cron-tools.js +128 -0
- package/dist/tools/cron-tools.js.map +1 -0
- package/dist/tools/edit.d.ts +5 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +70 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/glob.d.ts +5 -0
- package/dist/tools/glob.d.ts.map +1 -0
- package/dist/tools/glob.js +75 -0
- package/dist/tools/glob.js.map +1 -0
- package/dist/tools/grep.d.ts +5 -0
- package/dist/tools/grep.d.ts.map +1 -0
- package/dist/tools/grep.js +168 -0
- package/dist/tools/grep.js.map +1 -0
- package/dist/tools/index.d.ts +45 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +162 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/lsp-tool.d.ts +9 -0
- package/dist/tools/lsp-tool.d.ts.map +1 -0
- package/dist/tools/lsp-tool.js +137 -0
- package/dist/tools/lsp-tool.js.map +1 -0
- package/dist/tools/mcp-resource-tools.d.ts +14 -0
- package/dist/tools/mcp-resource-tools.d.ts.map +1 -0
- package/dist/tools/mcp-resource-tools.js +117 -0
- package/dist/tools/mcp-resource-tools.js.map +1 -0
- package/dist/tools/notebook-edit.d.ts +5 -0
- package/dist/tools/notebook-edit.d.ts.map +1 -0
- package/dist/tools/notebook-edit.js +85 -0
- package/dist/tools/notebook-edit.js.map +1 -0
- package/dist/tools/plan-tools.d.ts +12 -0
- package/dist/tools/plan-tools.d.ts.map +1 -0
- package/dist/tools/plan-tools.js +77 -0
- package/dist/tools/plan-tools.js.map +1 -0
- package/dist/tools/read.d.ts +5 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +66 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/send-message.d.ts +31 -0
- package/dist/tools/send-message.d.ts.map +1 -0
- package/dist/tools/send-message.js +77 -0
- package/dist/tools/send-message.js.map +1 -0
- package/dist/tools/skill-tool.d.ts +9 -0
- package/dist/tools/skill-tool.d.ts.map +1 -0
- package/dist/tools/skill-tool.js +115 -0
- package/dist/tools/skill-tool.js.map +1 -0
- package/dist/tools/task-tools.d.ts +48 -0
- package/dist/tools/task-tools.d.ts.map +1 -0
- package/dist/tools/task-tools.js +242 -0
- package/dist/tools/task-tools.js.map +1 -0
- package/dist/tools/team-tools.d.ts +34 -0
- package/dist/tools/team-tools.d.ts.map +1 -0
- package/dist/tools/team-tools.js +103 -0
- package/dist/tools/team-tools.js.map +1 -0
- package/dist/tools/todo-tool.d.ts +22 -0
- package/dist/tools/todo-tool.d.ts.map +1 -0
- package/dist/tools/todo-tool.js +93 -0
- package/dist/tools/todo-tool.js.map +1 -0
- package/dist/tools/tool-search.d.ts +13 -0
- package/dist/tools/tool-search.d.ts.map +1 -0
- package/dist/tools/tool-search.js +76 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/types.d.ts +29 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +52 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/web-fetch.d.ts +5 -0
- package/dist/tools/web-fetch.d.ts.map +1 -0
- package/dist/tools/web-fetch.js +60 -0
- package/dist/tools/web-fetch.js.map +1 -0
- package/dist/tools/web-search.d.ts +5 -0
- package/dist/tools/web-search.d.ts.map +1 -0
- package/dist/tools/web-search.js +77 -0
- package/dist/tools/web-search.js.map +1 -0
- package/dist/tools/worktree-tools.d.ts +10 -0
- package/dist/tools/worktree-tools.d.ts.map +1 -0
- package/dist/tools/worktree-tools.js +130 -0
- package/dist/tools/worktree-tools.js.map +1 -0
- package/dist/tools/write.d.ts +5 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +40 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/types.d.ts +423 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/compact.d.ts +44 -0
- package/dist/utils/compact.d.ts.map +1 -0
- package/dist/utils/compact.js +162 -0
- package/dist/utils/compact.js.map +1 -0
- package/dist/utils/context.d.ts +35 -0
- package/dist/utils/context.d.ts.map +1 -0
- package/dist/utils/context.js +174 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/fileCache.d.ts +64 -0
- package/dist/utils/fileCache.d.ts.map +1 -0
- package/dist/utils/fileCache.js +117 -0
- package/dist/utils/fileCache.js.map +1 -0
- package/dist/utils/messages.d.ts +57 -0
- package/dist/utils/messages.d.ts.map +1 -0
- package/dist/utils/messages.js +152 -0
- package/dist/utils/messages.js.map +1 -0
- package/dist/utils/retry.d.ts +48 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +111 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/tokens.d.ts +57 -0
- package/dist/utils/tokens.d.ts.map +1 -0
- package/dist/utils/tokens.js +134 -0
- package/dist/utils/tokens.js.map +1 -0
- package/package.json +7 -1
- package/.env.example +0 -8
- package/examples/01-simple-query.ts +0 -43
- package/examples/02-multi-tool.ts +0 -44
- package/examples/03-multi-turn.ts +0 -39
- package/examples/04-prompt-api.ts +0 -29
- package/examples/05-custom-system-prompt.ts +0 -26
- package/examples/06-mcp-server.ts +0 -49
- package/examples/07-custom-tools.ts +0 -87
- package/examples/08-official-api-compat.ts +0 -38
- package/examples/09-subagents.ts +0 -48
- package/examples/10-permissions.ts +0 -40
- package/examples/11-custom-mcp-tools.ts +0 -101
- package/examples/12-skills.ts +0 -88
- package/examples/13-hooks.ts +0 -88
- package/examples/14-openai-compat.ts +0 -71
- package/examples/web/index.html +0 -365
- package/examples/web/server.ts +0 -157
- package/tsconfig.json +0 -19
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates the appropriate provider based on API type configuration.
|
|
5
|
+
*/
|
|
6
|
+
export type { ApiType, LLMProvider, CreateMessageParams, CreateMessageResponse, NormalizedMessageParam, NormalizedContentBlock, NormalizedTool, NormalizedResponseBlock } from './types.js';
|
|
7
|
+
export { AnthropicProvider } from './anthropic.js';
|
|
8
|
+
export { OpenAIProvider } from './openai.js';
|
|
9
|
+
import type { ApiType, LLMProvider } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Create an LLM provider based on the API type.
|
|
12
|
+
*
|
|
13
|
+
* @param apiType - 'anthropic-messages' or 'openai-completions'
|
|
14
|
+
* @param opts - API credentials
|
|
15
|
+
*/
|
|
16
|
+
export declare function createProvider(apiType: ApiType, opts: {
|
|
17
|
+
apiKey?: string;
|
|
18
|
+
baseURL?: string;
|
|
19
|
+
}): LLMProvider;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAE3L,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAItD;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAC1C,WAAW,CASb"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider Factory
|
|
3
|
+
*
|
|
4
|
+
* Creates the appropriate provider based on API type configuration.
|
|
5
|
+
*/
|
|
6
|
+
export { AnthropicProvider } from './anthropic.js';
|
|
7
|
+
export { OpenAIProvider } from './openai.js';
|
|
8
|
+
import { AnthropicProvider } from './anthropic.js';
|
|
9
|
+
import { OpenAIProvider } from './openai.js';
|
|
10
|
+
/**
|
|
11
|
+
* Create an LLM provider based on the API type.
|
|
12
|
+
*
|
|
13
|
+
* @param apiType - 'anthropic-messages' or 'openai-completions'
|
|
14
|
+
* @param opts - API credentials
|
|
15
|
+
*/
|
|
16
|
+
export function createProvider(apiType, opts) {
|
|
17
|
+
switch (apiType) {
|
|
18
|
+
case 'anthropic-messages':
|
|
19
|
+
return new AnthropicProvider(opts);
|
|
20
|
+
case 'openai-completions':
|
|
21
|
+
return new OpenAIProvider(opts);
|
|
22
|
+
default:
|
|
23
|
+
throw new Error(`Unsupported API type: ${apiType}. Use 'anthropic-messages' or 'openai-completions'.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/providers/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAG5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAgB,EAChB,IAA2C;IAE3C,QAAQ,OAAO,EAAE,CAAC;QAChB,KAAK,oBAAoB;YACvB,OAAO,IAAI,iBAAiB,CAAC,IAAI,CAAC,CAAA;QACpC,KAAK,oBAAoB;YACvB,OAAO,IAAI,cAAc,CAAC,IAAI,CAAC,CAAA;QACjC;YACE,MAAM,IAAI,KAAK,CAAC,yBAAyB,OAAO,qDAAqD,CAAC,CAAA;IAC1G,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Chat Completions API Provider
|
|
3
|
+
*
|
|
4
|
+
* Converts between the SDK's internal Anthropic-like message format
|
|
5
|
+
* and OpenAI's Chat Completions API format.
|
|
6
|
+
*
|
|
7
|
+
* Uses native fetch (no openai SDK dependency required).
|
|
8
|
+
*/
|
|
9
|
+
import type { LLMProvider, CreateMessageParams, CreateMessageResponse } from './types.js';
|
|
10
|
+
export declare class OpenAIProvider implements LLMProvider {
|
|
11
|
+
readonly apiType: "openai-completions";
|
|
12
|
+
private apiKey;
|
|
13
|
+
private baseURL;
|
|
14
|
+
constructor(opts: {
|
|
15
|
+
apiKey?: string;
|
|
16
|
+
baseURL?: string;
|
|
17
|
+
});
|
|
18
|
+
createMessage(params: CreateMessageParams): Promise<CreateMessageResponse>;
|
|
19
|
+
private convertMessages;
|
|
20
|
+
private convertUserMessage;
|
|
21
|
+
private convertAssistantMessage;
|
|
22
|
+
private convertTools;
|
|
23
|
+
private convertResponse;
|
|
24
|
+
private mapFinishReason;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,mBAAmB,EACnB,qBAAqB,EAKtB,MAAM,YAAY,CAAA;AAqDnB,qBAAa,cAAe,YAAW,WAAW;IAChD,QAAQ,CAAC,OAAO,EAAG,oBAAoB,CAAS;IAChD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,OAAO,CAAQ;gBAEX,IAAI,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE;IAKjD,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,qBAAqB,CAAC;IA4ChF,OAAO,CAAC,eAAe;IAsBvB,OAAO,CAAC,kBAAkB;IAuC1B,OAAO,CAAC,uBAAuB;IA8C/B,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,eAAe;IAsDvB,OAAO,CAAC,eAAe;CAcxB"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Chat Completions API Provider
|
|
3
|
+
*
|
|
4
|
+
* Converts between the SDK's internal Anthropic-like message format
|
|
5
|
+
* and OpenAI's Chat Completions API format.
|
|
6
|
+
*
|
|
7
|
+
* Uses native fetch (no openai SDK dependency required).
|
|
8
|
+
*/
|
|
9
|
+
// --------------------------------------------------------------------------
|
|
10
|
+
// Provider
|
|
11
|
+
// --------------------------------------------------------------------------
|
|
12
|
+
export class OpenAIProvider {
|
|
13
|
+
apiType = 'openai-completions';
|
|
14
|
+
apiKey;
|
|
15
|
+
baseURL;
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
this.apiKey = opts.apiKey || '';
|
|
18
|
+
this.baseURL = (opts.baseURL || 'https://api.openai.com/v1').replace(/\/$/, '');
|
|
19
|
+
}
|
|
20
|
+
async createMessage(params) {
|
|
21
|
+
// Convert to OpenAI format
|
|
22
|
+
const messages = this.convertMessages(params.system, params.messages);
|
|
23
|
+
const tools = params.tools ? this.convertTools(params.tools) : undefined;
|
|
24
|
+
const body = {
|
|
25
|
+
model: params.model,
|
|
26
|
+
max_tokens: params.maxTokens,
|
|
27
|
+
messages,
|
|
28
|
+
};
|
|
29
|
+
if (tools && tools.length > 0) {
|
|
30
|
+
body.tools = tools;
|
|
31
|
+
}
|
|
32
|
+
// Make API call
|
|
33
|
+
const response = await fetch(`${this.baseURL}/chat/completions`, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: {
|
|
36
|
+
'Content-Type': 'application/json',
|
|
37
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
38
|
+
},
|
|
39
|
+
body: JSON.stringify(body),
|
|
40
|
+
});
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
const errBody = await response.text().catch(() => '');
|
|
43
|
+
const err = new Error(`OpenAI API error: ${response.status} ${response.statusText}: ${errBody}`);
|
|
44
|
+
err.status = response.status;
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
const data = (await response.json());
|
|
48
|
+
// Convert response back to normalized format
|
|
49
|
+
return this.convertResponse(data);
|
|
50
|
+
}
|
|
51
|
+
// --------------------------------------------------------------------------
|
|
52
|
+
// Message Conversion: Internal → OpenAI
|
|
53
|
+
// --------------------------------------------------------------------------
|
|
54
|
+
convertMessages(system, messages) {
|
|
55
|
+
const result = [];
|
|
56
|
+
// System prompt as first message
|
|
57
|
+
if (system) {
|
|
58
|
+
result.push({ role: 'system', content: system });
|
|
59
|
+
}
|
|
60
|
+
for (const msg of messages) {
|
|
61
|
+
if (msg.role === 'user') {
|
|
62
|
+
this.convertUserMessage(msg, result);
|
|
63
|
+
}
|
|
64
|
+
else if (msg.role === 'assistant') {
|
|
65
|
+
this.convertAssistantMessage(msg, result);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
convertUserMessage(msg, result) {
|
|
71
|
+
if (typeof msg.content === 'string') {
|
|
72
|
+
result.push({ role: 'user', content: msg.content });
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// Content blocks may contain text and/or tool_result blocks
|
|
76
|
+
const textParts = [];
|
|
77
|
+
const toolResults = [];
|
|
78
|
+
for (const block of msg.content) {
|
|
79
|
+
if (block.type === 'text') {
|
|
80
|
+
textParts.push(block.text);
|
|
81
|
+
}
|
|
82
|
+
else if (block.type === 'tool_result') {
|
|
83
|
+
toolResults.push({
|
|
84
|
+
tool_use_id: block.tool_use_id,
|
|
85
|
+
content: block.content,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Tool results become separate tool messages
|
|
90
|
+
for (const tr of toolResults) {
|
|
91
|
+
result.push({
|
|
92
|
+
role: 'tool',
|
|
93
|
+
tool_call_id: tr.tool_use_id,
|
|
94
|
+
content: tr.content,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// Text parts become a user message
|
|
98
|
+
if (textParts.length > 0) {
|
|
99
|
+
result.push({ role: 'user', content: textParts.join('\n') });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
convertAssistantMessage(msg, result) {
|
|
103
|
+
if (typeof msg.content === 'string') {
|
|
104
|
+
result.push({ role: 'assistant', content: msg.content });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Extract text and tool_use blocks
|
|
108
|
+
const textParts = [];
|
|
109
|
+
const toolCalls = [];
|
|
110
|
+
for (const block of msg.content) {
|
|
111
|
+
if (block.type === 'text') {
|
|
112
|
+
textParts.push(block.text);
|
|
113
|
+
}
|
|
114
|
+
else if (block.type === 'tool_use') {
|
|
115
|
+
toolCalls.push({
|
|
116
|
+
id: block.id,
|
|
117
|
+
type: 'function',
|
|
118
|
+
function: {
|
|
119
|
+
name: block.name,
|
|
120
|
+
arguments: typeof block.input === 'string'
|
|
121
|
+
? block.input
|
|
122
|
+
: JSON.stringify(block.input),
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const assistantMsg = {
|
|
128
|
+
role: 'assistant',
|
|
129
|
+
content: textParts.length > 0 ? textParts.join('\n') : null,
|
|
130
|
+
};
|
|
131
|
+
if (toolCalls.length > 0) {
|
|
132
|
+
assistantMsg.tool_calls = toolCalls;
|
|
133
|
+
}
|
|
134
|
+
result.push(assistantMsg);
|
|
135
|
+
}
|
|
136
|
+
// --------------------------------------------------------------------------
|
|
137
|
+
// Tool Conversion: Internal → OpenAI
|
|
138
|
+
// --------------------------------------------------------------------------
|
|
139
|
+
convertTools(tools) {
|
|
140
|
+
return tools.map((t) => ({
|
|
141
|
+
type: 'function',
|
|
142
|
+
function: {
|
|
143
|
+
name: t.name,
|
|
144
|
+
description: t.description,
|
|
145
|
+
parameters: t.input_schema,
|
|
146
|
+
},
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
// --------------------------------------------------------------------------
|
|
150
|
+
// Response Conversion: OpenAI → Internal
|
|
151
|
+
// --------------------------------------------------------------------------
|
|
152
|
+
convertResponse(data) {
|
|
153
|
+
const choice = data.choices[0];
|
|
154
|
+
if (!choice) {
|
|
155
|
+
return {
|
|
156
|
+
content: [{ type: 'text', text: '' }],
|
|
157
|
+
stopReason: 'end_turn',
|
|
158
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
const content = [];
|
|
162
|
+
// Add text content
|
|
163
|
+
if (choice.message.content) {
|
|
164
|
+
content.push({ type: 'text', text: choice.message.content });
|
|
165
|
+
}
|
|
166
|
+
// Add tool calls
|
|
167
|
+
if (choice.message.tool_calls) {
|
|
168
|
+
for (const tc of choice.message.tool_calls) {
|
|
169
|
+
let input;
|
|
170
|
+
try {
|
|
171
|
+
input = JSON.parse(tc.function.arguments);
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
input = tc.function.arguments;
|
|
175
|
+
}
|
|
176
|
+
content.push({
|
|
177
|
+
type: 'tool_use',
|
|
178
|
+
id: tc.id,
|
|
179
|
+
name: tc.function.name,
|
|
180
|
+
input,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// If no content at all, add empty text
|
|
185
|
+
if (content.length === 0) {
|
|
186
|
+
content.push({ type: 'text', text: '' });
|
|
187
|
+
}
|
|
188
|
+
// Map finish_reason to our normalized stop reasons
|
|
189
|
+
const stopReason = this.mapFinishReason(choice.finish_reason);
|
|
190
|
+
return {
|
|
191
|
+
content,
|
|
192
|
+
stopReason,
|
|
193
|
+
usage: {
|
|
194
|
+
input_tokens: data.usage?.prompt_tokens || 0,
|
|
195
|
+
output_tokens: data.usage?.completion_tokens || 0,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
mapFinishReason(reason) {
|
|
200
|
+
switch (reason) {
|
|
201
|
+
case 'stop':
|
|
202
|
+
return 'end_turn';
|
|
203
|
+
case 'length':
|
|
204
|
+
return 'max_tokens';
|
|
205
|
+
case 'tool_calls':
|
|
206
|
+
return 'tool_use';
|
|
207
|
+
default:
|
|
208
|
+
return reason;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/providers/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA2DH,6EAA6E;AAC7E,WAAW;AACX,6EAA6E;AAE7E,MAAM,OAAO,cAAc;IAChB,OAAO,GAAG,oBAA6B,CAAA;IACxC,MAAM,CAAQ;IACd,OAAO,CAAQ;IAEvB,YAAY,IAA2C;QACrD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,EAAE,CAAA;QAC/B,IAAI,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,OAAO,IAAI,2BAA2B,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;IACjF,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,MAA2B;QAC7C,2BAA2B;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC,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OAAO;YACP,UAAU;YACV,KAAK,EAAE;gBACL,YAAY,EAAE,IAAI,CAAC,KAAK,EAAE,aAAa,IAAI,CAAC;gBAC5C,aAAa,EAAE,IAAI,CAAC,KAAK,EAAE,iBAAiB,IAAI,CAAC;aAClD;SACF,CAAA;IACH,CAAC;IAEO,eAAe,CACrB,MAAc;QAEd,QAAQ,MAAM,EAAE,CAAC;YACf,KAAK,MAAM;gBACT,OAAO,UAAU,CAAA;YACnB,KAAK,QAAQ;gBACX,OAAO,YAAY,CAAA;YACrB,KAAK,YAAY;gBACf,OAAO,UAAU,CAAA;YACnB;gBACE,OAAO,MAAM,CAAA;QACjB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider Abstraction Types
|
|
3
|
+
*
|
|
4
|
+
* Defines a provider interface that normalizes API differences between
|
|
5
|
+
* Anthropic Messages API and OpenAI Chat Completions API.
|
|
6
|
+
*
|
|
7
|
+
* Internally the SDK uses Anthropic-like message format as the canonical
|
|
8
|
+
* representation. Providers convert to/from their native API format.
|
|
9
|
+
*/
|
|
10
|
+
export type ApiType = 'anthropic-messages' | 'openai-completions';
|
|
11
|
+
export interface CreateMessageParams {
|
|
12
|
+
model: string;
|
|
13
|
+
maxTokens: number;
|
|
14
|
+
system: string;
|
|
15
|
+
messages: NormalizedMessageParam[];
|
|
16
|
+
tools?: NormalizedTool[];
|
|
17
|
+
thinking?: {
|
|
18
|
+
type: string;
|
|
19
|
+
budget_tokens?: number;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Normalized message format (Anthropic-like).
|
|
24
|
+
* This is the internal representation used throughout the SDK.
|
|
25
|
+
*/
|
|
26
|
+
export interface NormalizedMessageParam {
|
|
27
|
+
role: 'user' | 'assistant';
|
|
28
|
+
content: string | NormalizedContentBlock[];
|
|
29
|
+
}
|
|
30
|
+
export type NormalizedContentBlock = {
|
|
31
|
+
type: 'text';
|
|
32
|
+
text: string;
|
|
33
|
+
} | {
|
|
34
|
+
type: 'tool_use';
|
|
35
|
+
id: string;
|
|
36
|
+
name: string;
|
|
37
|
+
input: any;
|
|
38
|
+
} | {
|
|
39
|
+
type: 'tool_result';
|
|
40
|
+
tool_use_id: string;
|
|
41
|
+
content: string;
|
|
42
|
+
is_error?: boolean;
|
|
43
|
+
} | {
|
|
44
|
+
type: 'image';
|
|
45
|
+
source: any;
|
|
46
|
+
} | {
|
|
47
|
+
type: 'thinking';
|
|
48
|
+
thinking: string;
|
|
49
|
+
};
|
|
50
|
+
export interface NormalizedTool {
|
|
51
|
+
name: string;
|
|
52
|
+
description: string;
|
|
53
|
+
input_schema: {
|
|
54
|
+
type: 'object';
|
|
55
|
+
properties: Record<string, any>;
|
|
56
|
+
required?: string[];
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export interface CreateMessageResponse {
|
|
60
|
+
content: NormalizedResponseBlock[];
|
|
61
|
+
stopReason: 'end_turn' | 'max_tokens' | 'tool_use' | string;
|
|
62
|
+
usage: {
|
|
63
|
+
input_tokens: number;
|
|
64
|
+
output_tokens: number;
|
|
65
|
+
cache_creation_input_tokens?: number;
|
|
66
|
+
cache_read_input_tokens?: number;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export type NormalizedResponseBlock = {
|
|
70
|
+
type: 'text';
|
|
71
|
+
text: string;
|
|
72
|
+
} | {
|
|
73
|
+
type: 'tool_use';
|
|
74
|
+
id: string;
|
|
75
|
+
name: string;
|
|
76
|
+
input: any;
|
|
77
|
+
};
|
|
78
|
+
export interface LLMProvider {
|
|
79
|
+
/** The API type this provider implements. */
|
|
80
|
+
readonly apiType: ApiType;
|
|
81
|
+
/** Send a message and get a response. */
|
|
82
|
+
createMessage(params: CreateMessageParams): Promise<CreateMessageResponse>;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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AAQ,CAAA;QACd,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;QAC/B,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;KACpB,CAAA;CACF;AAMD,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,uBAAuB,EAAE,CAAA;IAClC,UAAU,EAAE,UAAU,GAAG,YAAY,GAAG,UAAU,GAAG,MAAM,CAAA;IAC3D,KAAK,EAAE;QACL,YAAY,EAAE,MAAM,CAAA;QACpB,aAAa,EAAE,MAAM,CAAA;QACrB,2BAA2B,CAAC,EAAE,MAAM,CAAA;QACpC,uBAAuB,CAAC,EAAE,MAAM,CAAA;KACjC,CAAA;CACF;AAED,MAAM,MAAM,uBAAuB,GAC/B;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,GAAG,CAAA;CAAE,CAAA;AAM9D,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;IAEzB,yCAAyC;IACzC,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAA;CAC3E"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider Abstraction Types
|
|
3
|
+
*
|
|
4
|
+
* Defines a provider interface that normalizes API differences between
|
|
5
|
+
* Anthropic Messages API and OpenAI Chat Completions API.
|
|
6
|
+
*
|
|
7
|
+
* Internally the SDK uses Anthropic-like message format as the canonical
|
|
8
|
+
* representation. Providers convert to/from their native API format.
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/providers/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-Process MCP Server
|
|
3
|
+
*
|
|
4
|
+
* createSdkMcpServer() creates an in-process MCP server from tool() definitions.
|
|
5
|
+
* Compatible with open-agent-sdk's createSdkMcpServer().
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { tool, createSdkMcpServer } from 'open-agent-sdk'
|
|
9
|
+
* import { z } from 'zod'
|
|
10
|
+
*
|
|
11
|
+
* const weatherTool = tool('get_weather', 'Get weather', { city: z.string() },
|
|
12
|
+
* async ({ city }) => ({ content: [{ type: 'text', text: `22°C in ${city}` }] })
|
|
13
|
+
* )
|
|
14
|
+
*
|
|
15
|
+
* const server = createSdkMcpServer({
|
|
16
|
+
* name: 'weather',
|
|
17
|
+
* tools: [weatherTool],
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* // Use as MCP server config:
|
|
21
|
+
* const agent = createAgent({
|
|
22
|
+
* mcpServers: { weather: server },
|
|
23
|
+
* })
|
|
24
|
+
*/
|
|
25
|
+
import type { SdkMcpToolDefinition } from './tool-helper.js';
|
|
26
|
+
import type { ToolDefinition } from './types.js';
|
|
27
|
+
/**
|
|
28
|
+
* SDK MCP server config that includes the in-process server instance.
|
|
29
|
+
*/
|
|
30
|
+
export interface McpSdkServerConfig {
|
|
31
|
+
type: 'sdk';
|
|
32
|
+
name: string;
|
|
33
|
+
version: string;
|
|
34
|
+
tools: ToolDefinition[];
|
|
35
|
+
_sdkTools: SdkMcpToolDefinition<any>[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create an in-process MCP server from tool definitions.
|
|
39
|
+
*
|
|
40
|
+
* The server runs in the same process as the agent, avoiding
|
|
41
|
+
* subprocess overhead. Tools are directly callable.
|
|
42
|
+
*/
|
|
43
|
+
export declare function createSdkMcpServer(options: {
|
|
44
|
+
name: string;
|
|
45
|
+
version?: string;
|
|
46
|
+
tools?: SdkMcpToolDefinition<any>[];
|
|
47
|
+
}): McpSdkServerConfig;
|
|
48
|
+
/**
|
|
49
|
+
* Check if a server config is an in-process SDK server.
|
|
50
|
+
*/
|
|
51
|
+
export declare function isSdkServerConfig(config: any): config is McpSdkServerConfig;
|
|
52
|
+
//# sourceMappingURL=sdk-mcp-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk-mcp-server.d.ts","sourceRoot":"","sources":["../src/sdk-mcp-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAE5D,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,YAAY,CAAA;AAEjE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,KAAK,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,cAAc,EAAE,CAAA;IACvB,SAAS,EAAE,oBAAoB,CAAC,GAAG,CAAC,EAAE,CAAA;CACvC;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE;IAC1C,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,oBAAoB,CAAC,GAAG,CAAC,EAAE,CAAA;CACpC,GAAG,kBAAkB,CAoBrB;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,GAAG,GAAG,MAAM,IAAI,kBAAkB,CAE3E"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-Process MCP Server
|
|
3
|
+
*
|
|
4
|
+
* createSdkMcpServer() creates an in-process MCP server from tool() definitions.
|
|
5
|
+
* Compatible with open-agent-sdk's createSdkMcpServer().
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { tool, createSdkMcpServer } from 'open-agent-sdk'
|
|
9
|
+
* import { z } from 'zod'
|
|
10
|
+
*
|
|
11
|
+
* const weatherTool = tool('get_weather', 'Get weather', { city: z.string() },
|
|
12
|
+
* async ({ city }) => ({ content: [{ type: 'text', text: `22°C in ${city}` }] })
|
|
13
|
+
* )
|
|
14
|
+
*
|
|
15
|
+
* const server = createSdkMcpServer({
|
|
16
|
+
* name: 'weather',
|
|
17
|
+
* tools: [weatherTool],
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* // Use as MCP server config:
|
|
21
|
+
* const agent = createAgent({
|
|
22
|
+
* mcpServers: { weather: server },
|
|
23
|
+
* })
|
|
24
|
+
*/
|
|
25
|
+
import { sdkToolToToolDefinition } from './tool-helper.js';
|
|
26
|
+
/**
|
|
27
|
+
* Create an in-process MCP server from tool definitions.
|
|
28
|
+
*
|
|
29
|
+
* The server runs in the same process as the agent, avoiding
|
|
30
|
+
* subprocess overhead. Tools are directly callable.
|
|
31
|
+
*/
|
|
32
|
+
export function createSdkMcpServer(options) {
|
|
33
|
+
const sdkTools = options.tools || [];
|
|
34
|
+
// Convert SDK tools to engine-compatible tool definitions
|
|
35
|
+
// Prefix tool names with mcp__{server_name}__ for namespace isolation
|
|
36
|
+
const toolDefinitions = sdkTools.map((sdkTool) => {
|
|
37
|
+
const toolDef = sdkToolToToolDefinition(sdkTool);
|
|
38
|
+
return {
|
|
39
|
+
...toolDef,
|
|
40
|
+
name: `mcp__${options.name}__${sdkTool.name}`,
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
type: 'sdk',
|
|
45
|
+
name: options.name,
|
|
46
|
+
version: options.version || '1.0.0',
|
|
47
|
+
tools: toolDefinitions,
|
|
48
|
+
_sdkTools: sdkTools,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check if a server config is an in-process SDK server.
|
|
53
|
+
*/
|
|
54
|
+
export function isSdkServerConfig(config) {
|
|
55
|
+
return config?.type === 'sdk' && Array.isArray(config.tools);
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=sdk-mcp-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk-mcp-server.js","sourceRoot":"","sources":["../src/sdk-mcp-server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAGH,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAc1D;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAIlC;IACC,MAAM,QAAQ,GAAG,OAAO,CAAC,KAAK,IAAI,EAAE,CAAA;IAEpC,0DAA0D;IAC1D,sEAAsE;IACtE,MAAM,eAAe,GAAqB,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QACjE,MAAM,OAAO,GAAG,uBAAuB,CAAC,OAAO,CAAC,CAAA;QAChD,OAAO;YACL,GAAG,OAAO;YACV,IAAI,EAAE,QAAQ,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,EAAE;SAC9C,CAAA;IACH,CAAC,CAAC,CAAA;IAEF,OAAO;QACL,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,OAAO;QACnC,KAAK,EAAE,eAAe;QACtB,SAAS,EAAE,QAAQ;KACpB,CAAA;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAW;IAC3C,OAAO,MAAM,EAAE,IAAI,KAAK,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC9D,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Storage & Management
|
|
3
|
+
*
|
|
4
|
+
* Persists conversation transcripts to disk for resumption.
|
|
5
|
+
* Manages session lifecycle (create, resume, list, fork).
|
|
6
|
+
*/
|
|
7
|
+
import type { NormalizedMessageParam } from './providers/types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Session metadata.
|
|
10
|
+
*/
|
|
11
|
+
export interface SessionMetadata {
|
|
12
|
+
id: string;
|
|
13
|
+
cwd: string;
|
|
14
|
+
model: string;
|
|
15
|
+
createdAt: string;
|
|
16
|
+
updatedAt: string;
|
|
17
|
+
messageCount: number;
|
|
18
|
+
summary?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Session data on disk.
|
|
22
|
+
*/
|
|
23
|
+
export interface SessionData {
|
|
24
|
+
metadata: SessionMetadata;
|
|
25
|
+
messages: NormalizedMessageParam[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Save session to disk.
|
|
29
|
+
*/
|
|
30
|
+
export declare function saveSession(sessionId: string, messages: NormalizedMessageParam[], metadata: Partial<SessionMetadata>): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Load session from disk.
|
|
33
|
+
*/
|
|
34
|
+
export declare function loadSession(sessionId: string): Promise<SessionData | null>;
|
|
35
|
+
/**
|
|
36
|
+
* List all sessions.
|
|
37
|
+
*/
|
|
38
|
+
export declare function listSessions(): Promise<SessionMetadata[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Fork a session (create a copy with a new ID).
|
|
41
|
+
*/
|
|
42
|
+
export declare function forkSession(sourceSessionId: string, newSessionId?: string): Promise<string | null>;
|
|
43
|
+
/**
|
|
44
|
+
* Get session messages.
|
|
45
|
+
*/
|
|
46
|
+
export declare function getSessionMessages(sessionId: string): Promise<NormalizedMessageParam[]>;
|
|
47
|
+
/**
|
|
48
|
+
* Append a message to a session transcript.
|
|
49
|
+
*/
|
|
50
|
+
export declare function appendToSession(sessionId: string, message: NormalizedMessageParam): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Delete a session.
|
|
53
|
+
*/
|
|
54
|
+
export declare function deleteSession(sessionId: string): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Get info about a specific session.
|
|
57
|
+
*/
|
|
58
|
+
export declare function getSessionInfo(sessionId: string, options?: {
|
|
59
|
+
dir?: string;
|
|
60
|
+
}): Promise<SessionMetadata | null>;
|
|
61
|
+
/**
|
|
62
|
+
* Rename a session.
|
|
63
|
+
*/
|
|
64
|
+
export declare function renameSession(sessionId: string, title: string, options?: {
|
|
65
|
+
dir?: string;
|
|
66
|
+
}): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Tag a session.
|
|
69
|
+
*/
|
|
70
|
+
export declare function tagSession(sessionId: string, tag: string | null, options?: {
|
|
71
|
+
dir?: string;
|
|
72
|
+
}): Promise<void>;
|
|
73
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AAElE;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAA;IACV,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,eAAe,CAAA;IACzB,QAAQ,EAAE,sBAAsB,EAAE,CAAA;CACnC;AAiBD;;GAEG;AACH,wBAAsB,WAAW,CAC/B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,sBAAsB,EAAE,EAClC,QAAQ,EAAE,OAAO,CAAC,eAAe,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC,C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