@shareai-lab/kode-sdk 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +312 -0
- package/dist/core/agent.d.ts +85 -0
- package/dist/core/agent.js +687 -0
- package/dist/core/events.d.ts +19 -0
- package/dist/core/events.js +121 -0
- package/dist/core/hooks.d.ts +23 -0
- package/dist/core/hooks.js +71 -0
- package/dist/core/pool.d.ts +33 -0
- package/dist/core/pool.js +91 -0
- package/dist/core/room.d.ts +15 -0
- package/dist/core/room.js +57 -0
- package/dist/core/scheduler.d.ts +26 -0
- package/dist/core/scheduler.js +184 -0
- package/dist/core/types.d.ts +192 -0
- package/dist/core/types.js +13 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +57 -0
- package/dist/infra/provider.d.ts +58 -0
- package/dist/infra/provider.js +118 -0
- package/dist/infra/sandbox.d.ts +39 -0
- package/dist/infra/sandbox.js +77 -0
- package/dist/infra/store.d.ts +32 -0
- package/dist/infra/store.js +132 -0
- package/dist/tools/bash.d.ts +63 -0
- package/dist/tools/bash.js +99 -0
- package/dist/tools/builtin.d.ts +15 -0
- package/dist/tools/builtin.js +96 -0
- package/dist/tools/fs.d.ts +96 -0
- package/dist/tools/fs.js +96 -0
- package/dist/tools/task.d.ts +38 -0
- package/dist/tools/task.js +45 -0
- package/dist/utils/session-id.d.ts +21 -0
- package/dist/utils/session-id.js +64 -0
- package/package.json +47 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
export type ContentBlock = {
|
|
2
|
+
type: 'text';
|
|
3
|
+
text: string;
|
|
4
|
+
} | {
|
|
5
|
+
type: 'tool_use';
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
input: any;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'tool_result';
|
|
11
|
+
tool_use_id: string;
|
|
12
|
+
content: any;
|
|
13
|
+
is_error?: boolean;
|
|
14
|
+
};
|
|
15
|
+
export type MessageRole = 'user' | 'assistant' | 'system';
|
|
16
|
+
export interface Message {
|
|
17
|
+
role: MessageRole;
|
|
18
|
+
content: ContentBlock[];
|
|
19
|
+
}
|
|
20
|
+
export type AgentState = 'READY' | 'PAUSED' | 'BUSY';
|
|
21
|
+
export type ErrorKind = 'ProviderError' | 'ToolTimeout' | 'ToolDenied' | 'PermissionPending' | 'PolicyViolation' | 'StoreError' | 'MCPError';
|
|
22
|
+
export type AgentEventKind = 'text_chunk' | 'text' | 'tool_use' | 'tool_result' | 'usage' | 'error' | 'messages_update' | 'permission_ask' | 'permission_decision' | 'commit' | 'state' | 'resume' | 'forked';
|
|
23
|
+
export type AgentEvent = {
|
|
24
|
+
type: 'text_chunk';
|
|
25
|
+
cursor: number;
|
|
26
|
+
eventId: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
delta: string;
|
|
29
|
+
} | {
|
|
30
|
+
type: 'text';
|
|
31
|
+
cursor: number;
|
|
32
|
+
eventId: string;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
text: string;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'tool_use';
|
|
37
|
+
cursor: number;
|
|
38
|
+
eventId: string;
|
|
39
|
+
timestamp: number;
|
|
40
|
+
id: string;
|
|
41
|
+
name: string;
|
|
42
|
+
input: any;
|
|
43
|
+
} | {
|
|
44
|
+
type: 'tool_result';
|
|
45
|
+
cursor: number;
|
|
46
|
+
eventId: string;
|
|
47
|
+
timestamp: number;
|
|
48
|
+
id: string;
|
|
49
|
+
name: string;
|
|
50
|
+
ok: boolean;
|
|
51
|
+
content: any;
|
|
52
|
+
duration_ms?: number;
|
|
53
|
+
} | {
|
|
54
|
+
type: 'usage';
|
|
55
|
+
cursor: number;
|
|
56
|
+
eventId: string;
|
|
57
|
+
timestamp: number;
|
|
58
|
+
data: {
|
|
59
|
+
input_tokens: number;
|
|
60
|
+
output_tokens: number;
|
|
61
|
+
total_tokens: number;
|
|
62
|
+
cost_usd?: number;
|
|
63
|
+
latency_ms?: number;
|
|
64
|
+
};
|
|
65
|
+
} | {
|
|
66
|
+
type: 'error';
|
|
67
|
+
cursor: number;
|
|
68
|
+
eventId: string;
|
|
69
|
+
timestamp: number;
|
|
70
|
+
kind: ErrorKind;
|
|
71
|
+
message: string;
|
|
72
|
+
hint?: string;
|
|
73
|
+
} | {
|
|
74
|
+
type: 'messages_update';
|
|
75
|
+
cursor: number;
|
|
76
|
+
eventId: string;
|
|
77
|
+
timestamp: number;
|
|
78
|
+
messageCount: number;
|
|
79
|
+
lastSfpIndex: number;
|
|
80
|
+
added?: number;
|
|
81
|
+
} | {
|
|
82
|
+
type: 'permission_ask';
|
|
83
|
+
cursor: number;
|
|
84
|
+
eventId: string;
|
|
85
|
+
timestamp: number;
|
|
86
|
+
id: string;
|
|
87
|
+
tool: string;
|
|
88
|
+
args: any;
|
|
89
|
+
meta?: any;
|
|
90
|
+
respond: (decision: 'allow' | 'deny', note?: string) => Promise<void>;
|
|
91
|
+
} | {
|
|
92
|
+
type: 'permission_decision';
|
|
93
|
+
cursor: number;
|
|
94
|
+
eventId: string;
|
|
95
|
+
timestamp: number;
|
|
96
|
+
id: string;
|
|
97
|
+
decision: 'allow' | 'deny';
|
|
98
|
+
by: 'api' | 'respond';
|
|
99
|
+
} | {
|
|
100
|
+
type: 'commit';
|
|
101
|
+
cursor: number;
|
|
102
|
+
eventId: string;
|
|
103
|
+
timestamp: number;
|
|
104
|
+
sfpIndex: number;
|
|
105
|
+
} | {
|
|
106
|
+
type: 'state';
|
|
107
|
+
cursor: number;
|
|
108
|
+
eventId: string;
|
|
109
|
+
timestamp: number;
|
|
110
|
+
state: AgentState;
|
|
111
|
+
} | {
|
|
112
|
+
type: 'resume';
|
|
113
|
+
cursor: number;
|
|
114
|
+
eventId: string;
|
|
115
|
+
timestamp: number;
|
|
116
|
+
from: 'crash' | 'manual';
|
|
117
|
+
sealed: Array<{
|
|
118
|
+
tool_use_id: string;
|
|
119
|
+
note: string;
|
|
120
|
+
}>;
|
|
121
|
+
} | {
|
|
122
|
+
type: 'forked';
|
|
123
|
+
cursor: number;
|
|
124
|
+
eventId: string;
|
|
125
|
+
timestamp: number;
|
|
126
|
+
childSessionId: string;
|
|
127
|
+
from: SnapshotId;
|
|
128
|
+
};
|
|
129
|
+
export declare const MINIMAL_EVENT_KINDS: AgentEventKind[];
|
|
130
|
+
export interface Timeline {
|
|
131
|
+
cursor: number;
|
|
132
|
+
event: AgentEvent;
|
|
133
|
+
}
|
|
134
|
+
export type SnapshotId = string;
|
|
135
|
+
export interface Snapshot {
|
|
136
|
+
id: SnapshotId;
|
|
137
|
+
messages: Message[];
|
|
138
|
+
lastSfpIndex: number;
|
|
139
|
+
createdAt: string;
|
|
140
|
+
}
|
|
141
|
+
export interface SubscribeOptions {
|
|
142
|
+
since?: number;
|
|
143
|
+
kinds?: AgentEventKind[];
|
|
144
|
+
}
|
|
145
|
+
export interface AgentStatus {
|
|
146
|
+
state: AgentState;
|
|
147
|
+
sessionId: string;
|
|
148
|
+
messageCount: number;
|
|
149
|
+
lastSfpIndex: number;
|
|
150
|
+
cursor: number;
|
|
151
|
+
}
|
|
152
|
+
export interface AgentInfo {
|
|
153
|
+
sessionId: string;
|
|
154
|
+
templateId: string;
|
|
155
|
+
createdAt: string;
|
|
156
|
+
lineage: string[];
|
|
157
|
+
messageCount: number;
|
|
158
|
+
lastSfpIndex: number;
|
|
159
|
+
}
|
|
160
|
+
export interface ToolCall {
|
|
161
|
+
id: string;
|
|
162
|
+
name: string;
|
|
163
|
+
args: any;
|
|
164
|
+
sessionId: string;
|
|
165
|
+
}
|
|
166
|
+
export interface ToolOutcome {
|
|
167
|
+
id: string;
|
|
168
|
+
name: string;
|
|
169
|
+
ok: boolean;
|
|
170
|
+
content: any;
|
|
171
|
+
duration_ms?: number;
|
|
172
|
+
}
|
|
173
|
+
export type HookDecision = {
|
|
174
|
+
decision: 'ask';
|
|
175
|
+
meta?: any;
|
|
176
|
+
} | {
|
|
177
|
+
decision: 'deny';
|
|
178
|
+
reason?: string;
|
|
179
|
+
toolResult?: any;
|
|
180
|
+
} | {
|
|
181
|
+
result: any;
|
|
182
|
+
} | void;
|
|
183
|
+
export type PostHookResult = void | {
|
|
184
|
+
update: Partial<ToolOutcome>;
|
|
185
|
+
} | {
|
|
186
|
+
replace: ToolOutcome;
|
|
187
|
+
};
|
|
188
|
+
export interface ToolContext {
|
|
189
|
+
sessionId: string;
|
|
190
|
+
sandbox: import('../infra/sandbox').Sandbox;
|
|
191
|
+
agent: any;
|
|
192
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Core data structures for Agent Model Client SDK
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.MINIMAL_EVENT_KINDS = void 0;
|
|
5
|
+
exports.MINIMAL_EVENT_KINDS = [
|
|
6
|
+
'text_chunk',
|
|
7
|
+
'text',
|
|
8
|
+
'tool_use',
|
|
9
|
+
'tool_result',
|
|
10
|
+
'usage',
|
|
11
|
+
'error',
|
|
12
|
+
'messages_update',
|
|
13
|
+
];
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { Agent, AgentOptions } from './core/agent';
|
|
2
|
+
export { AgentPool } from './core/pool';
|
|
3
|
+
export { Room } from './core/room';
|
|
4
|
+
export { Scheduler, AgentSchedulerHandle } from './core/scheduler';
|
|
5
|
+
export { EventBus } from './core/events';
|
|
6
|
+
export { HookManager, Hooks } from './core/hooks';
|
|
7
|
+
export * from './core/types';
|
|
8
|
+
export { Store, JSONStore } from './infra/store';
|
|
9
|
+
export { Sandbox, LocalSandbox } from './infra/sandbox';
|
|
10
|
+
export { Provider, AnthropicProvider } from './infra/provider';
|
|
11
|
+
export { Tool, FsRead, FsWrite, FsEdit, toolTune } from './tools/fs';
|
|
12
|
+
export { BashRun, BashLogs, BashKill } from './tools/bash';
|
|
13
|
+
export { TaskRun, AgentTemplate } from './tools/task';
|
|
14
|
+
export { builtin } from './tools/builtin';
|
|
15
|
+
export { SessionId } from './utils/session-id';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.SessionId = exports.builtin = exports.TaskRun = exports.BashKill = exports.BashLogs = exports.BashRun = exports.toolTune = exports.FsEdit = exports.FsWrite = exports.FsRead = exports.AnthropicProvider = exports.LocalSandbox = exports.JSONStore = exports.HookManager = exports.EventBus = exports.AgentSchedulerHandle = exports.Scheduler = exports.Room = exports.AgentPool = exports.Agent = void 0;
|
|
18
|
+
// Core
|
|
19
|
+
var agent_1 = require("./core/agent");
|
|
20
|
+
Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return agent_1.Agent; } });
|
|
21
|
+
var pool_1 = require("./core/pool");
|
|
22
|
+
Object.defineProperty(exports, "AgentPool", { enumerable: true, get: function () { return pool_1.AgentPool; } });
|
|
23
|
+
var room_1 = require("./core/room");
|
|
24
|
+
Object.defineProperty(exports, "Room", { enumerable: true, get: function () { return room_1.Room; } });
|
|
25
|
+
var scheduler_1 = require("./core/scheduler");
|
|
26
|
+
Object.defineProperty(exports, "Scheduler", { enumerable: true, get: function () { return scheduler_1.Scheduler; } });
|
|
27
|
+
Object.defineProperty(exports, "AgentSchedulerHandle", { enumerable: true, get: function () { return scheduler_1.AgentSchedulerHandle; } });
|
|
28
|
+
var events_1 = require("./core/events");
|
|
29
|
+
Object.defineProperty(exports, "EventBus", { enumerable: true, get: function () { return events_1.EventBus; } });
|
|
30
|
+
var hooks_1 = require("./core/hooks");
|
|
31
|
+
Object.defineProperty(exports, "HookManager", { enumerable: true, get: function () { return hooks_1.HookManager; } });
|
|
32
|
+
// Types
|
|
33
|
+
__exportStar(require("./core/types"), exports);
|
|
34
|
+
// Infrastructure
|
|
35
|
+
var store_1 = require("./infra/store");
|
|
36
|
+
Object.defineProperty(exports, "JSONStore", { enumerable: true, get: function () { return store_1.JSONStore; } });
|
|
37
|
+
var sandbox_1 = require("./infra/sandbox");
|
|
38
|
+
Object.defineProperty(exports, "LocalSandbox", { enumerable: true, get: function () { return sandbox_1.LocalSandbox; } });
|
|
39
|
+
var provider_1 = require("./infra/provider");
|
|
40
|
+
Object.defineProperty(exports, "AnthropicProvider", { enumerable: true, get: function () { return provider_1.AnthropicProvider; } });
|
|
41
|
+
// Tools
|
|
42
|
+
var fs_1 = require("./tools/fs");
|
|
43
|
+
Object.defineProperty(exports, "FsRead", { enumerable: true, get: function () { return fs_1.FsRead; } });
|
|
44
|
+
Object.defineProperty(exports, "FsWrite", { enumerable: true, get: function () { return fs_1.FsWrite; } });
|
|
45
|
+
Object.defineProperty(exports, "FsEdit", { enumerable: true, get: function () { return fs_1.FsEdit; } });
|
|
46
|
+
Object.defineProperty(exports, "toolTune", { enumerable: true, get: function () { return fs_1.toolTune; } });
|
|
47
|
+
var bash_1 = require("./tools/bash");
|
|
48
|
+
Object.defineProperty(exports, "BashRun", { enumerable: true, get: function () { return bash_1.BashRun; } });
|
|
49
|
+
Object.defineProperty(exports, "BashLogs", { enumerable: true, get: function () { return bash_1.BashLogs; } });
|
|
50
|
+
Object.defineProperty(exports, "BashKill", { enumerable: true, get: function () { return bash_1.BashKill; } });
|
|
51
|
+
var task_1 = require("./tools/task");
|
|
52
|
+
Object.defineProperty(exports, "TaskRun", { enumerable: true, get: function () { return task_1.TaskRun; } });
|
|
53
|
+
var builtin_1 = require("./tools/builtin");
|
|
54
|
+
Object.defineProperty(exports, "builtin", { enumerable: true, get: function () { return builtin_1.builtin; } });
|
|
55
|
+
// Utils
|
|
56
|
+
var session_id_1 = require("./utils/session-id");
|
|
57
|
+
Object.defineProperty(exports, "SessionId", { enumerable: true, get: function () { return session_id_1.SessionId; } });
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Message, ContentBlock } from '../core/types';
|
|
2
|
+
export interface ProviderResponse {
|
|
3
|
+
role: 'assistant';
|
|
4
|
+
content: ContentBlock[];
|
|
5
|
+
usage?: {
|
|
6
|
+
input_tokens: number;
|
|
7
|
+
output_tokens: number;
|
|
8
|
+
};
|
|
9
|
+
stop_reason?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface ProviderStreamChunk {
|
|
12
|
+
type: 'content_block_start' | 'content_block_delta' | 'content_block_stop' | 'message_delta' | 'message_stop';
|
|
13
|
+
index?: number;
|
|
14
|
+
content_block?: ContentBlock;
|
|
15
|
+
delta?: {
|
|
16
|
+
type: 'text_delta' | 'input_json_delta';
|
|
17
|
+
text?: string;
|
|
18
|
+
partial_json?: string;
|
|
19
|
+
};
|
|
20
|
+
usage?: {
|
|
21
|
+
output_tokens: number;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export interface Provider {
|
|
25
|
+
complete(messages: Message[], opts?: {
|
|
26
|
+
tools?: any[];
|
|
27
|
+
maxTokens?: number;
|
|
28
|
+
temperature?: number;
|
|
29
|
+
system?: string;
|
|
30
|
+
stream?: boolean;
|
|
31
|
+
}): Promise<ProviderResponse>;
|
|
32
|
+
stream(messages: Message[], opts?: {
|
|
33
|
+
tools?: any[];
|
|
34
|
+
maxTokens?: number;
|
|
35
|
+
temperature?: number;
|
|
36
|
+
system?: string;
|
|
37
|
+
}): AsyncIterable<ProviderStreamChunk>;
|
|
38
|
+
}
|
|
39
|
+
export declare class AnthropicProvider implements Provider {
|
|
40
|
+
private apiKey;
|
|
41
|
+
private model;
|
|
42
|
+
private baseUrl;
|
|
43
|
+
constructor(apiKey: string, model?: string, baseUrl?: string);
|
|
44
|
+
complete(messages: Message[], opts?: {
|
|
45
|
+
tools?: any[];
|
|
46
|
+
maxTokens?: number;
|
|
47
|
+
temperature?: number;
|
|
48
|
+
system?: string;
|
|
49
|
+
stream?: boolean;
|
|
50
|
+
}): Promise<ProviderResponse>;
|
|
51
|
+
stream(messages: Message[], opts?: {
|
|
52
|
+
tools?: any[];
|
|
53
|
+
maxTokens?: number;
|
|
54
|
+
temperature?: number;
|
|
55
|
+
system?: string;
|
|
56
|
+
}): AsyncIterable<ProviderStreamChunk>;
|
|
57
|
+
private formatMessages;
|
|
58
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnthropicProvider = void 0;
|
|
4
|
+
class AnthropicProvider {
|
|
5
|
+
constructor(apiKey, model = 'claude-3-5-sonnet-20241022', baseUrl = 'https://api.anthropic.com') {
|
|
6
|
+
this.apiKey = apiKey;
|
|
7
|
+
this.model = model;
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
}
|
|
10
|
+
async complete(messages, opts) {
|
|
11
|
+
const body = {
|
|
12
|
+
model: this.model,
|
|
13
|
+
messages: this.formatMessages(messages),
|
|
14
|
+
max_tokens: opts?.maxTokens || 4096,
|
|
15
|
+
};
|
|
16
|
+
if (opts?.temperature !== undefined)
|
|
17
|
+
body.temperature = opts.temperature;
|
|
18
|
+
if (opts?.system)
|
|
19
|
+
body.system = opts.system;
|
|
20
|
+
if (opts?.tools && opts.tools.length > 0)
|
|
21
|
+
body.tools = opts.tools;
|
|
22
|
+
const response = await fetch(`${this.baseUrl}/v1/messages`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
'Content-Type': 'application/json',
|
|
26
|
+
'x-api-key': this.apiKey,
|
|
27
|
+
'anthropic-version': '2023-06-01',
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify(body),
|
|
30
|
+
});
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
const error = await response.text();
|
|
33
|
+
throw new Error(`Anthropic API error: ${response.status} ${error}`);
|
|
34
|
+
}
|
|
35
|
+
const data = await response.json();
|
|
36
|
+
return {
|
|
37
|
+
role: 'assistant',
|
|
38
|
+
content: data.content,
|
|
39
|
+
usage: data.usage,
|
|
40
|
+
stop_reason: data.stop_reason,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
async *stream(messages, opts) {
|
|
44
|
+
const body = {
|
|
45
|
+
model: this.model,
|
|
46
|
+
messages: this.formatMessages(messages),
|
|
47
|
+
max_tokens: opts?.maxTokens || 4096,
|
|
48
|
+
stream: true,
|
|
49
|
+
};
|
|
50
|
+
if (opts?.temperature !== undefined)
|
|
51
|
+
body.temperature = opts.temperature;
|
|
52
|
+
if (opts?.system)
|
|
53
|
+
body.system = opts.system;
|
|
54
|
+
if (opts?.tools && opts.tools.length > 0)
|
|
55
|
+
body.tools = opts.tools;
|
|
56
|
+
const response = await fetch(`${this.baseUrl}/v1/messages`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
'x-api-key': this.apiKey,
|
|
61
|
+
'anthropic-version': '2023-06-01',
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify(body),
|
|
64
|
+
});
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
const error = await response.text();
|
|
67
|
+
throw new Error(`Anthropic API error: ${response.status} ${error}`);
|
|
68
|
+
}
|
|
69
|
+
const reader = response.body?.getReader();
|
|
70
|
+
if (!reader)
|
|
71
|
+
throw new Error('No response body');
|
|
72
|
+
const decoder = new TextDecoder();
|
|
73
|
+
let buffer = '';
|
|
74
|
+
while (true) {
|
|
75
|
+
const { done, value } = await reader.read();
|
|
76
|
+
if (done)
|
|
77
|
+
break;
|
|
78
|
+
buffer += decoder.decode(value, { stream: true });
|
|
79
|
+
const lines = buffer.split('\n');
|
|
80
|
+
buffer = lines.pop() || '';
|
|
81
|
+
for (const line of lines) {
|
|
82
|
+
if (!line.trim() || !line.startsWith('data: '))
|
|
83
|
+
continue;
|
|
84
|
+
const data = line.slice(6);
|
|
85
|
+
if (data === '[DONE]')
|
|
86
|
+
continue;
|
|
87
|
+
try {
|
|
88
|
+
const event = JSON.parse(data);
|
|
89
|
+
if (event.type === 'content_block_start') {
|
|
90
|
+
yield { type: 'content_block_start', index: event.index, content_block: event.content_block };
|
|
91
|
+
}
|
|
92
|
+
else if (event.type === 'content_block_delta') {
|
|
93
|
+
yield { type: 'content_block_delta', index: event.index, delta: event.delta };
|
|
94
|
+
}
|
|
95
|
+
else if (event.type === 'content_block_stop') {
|
|
96
|
+
yield { type: 'content_block_stop', index: event.index };
|
|
97
|
+
}
|
|
98
|
+
else if (event.type === 'message_delta') {
|
|
99
|
+
yield { type: 'message_delta', delta: event.delta, usage: event.usage };
|
|
100
|
+
}
|
|
101
|
+
else if (event.type === 'message_stop') {
|
|
102
|
+
yield { type: 'message_stop' };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
// Skip invalid JSON
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
formatMessages(messages) {
|
|
112
|
+
return messages.map((msg) => ({
|
|
113
|
+
role: msg.role === 'system' ? 'user' : msg.role,
|
|
114
|
+
content: msg.content,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.AnthropicProvider = AnthropicProvider;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export type SandboxKind = 'local' | 'docker' | 'k8s' | 'remote' | 'vfs';
|
|
2
|
+
export interface SandboxFS {
|
|
3
|
+
resolve(path: string): string;
|
|
4
|
+
isInside(path: string): boolean;
|
|
5
|
+
read(path: string): Promise<string>;
|
|
6
|
+
write(path: string, content: string): Promise<void>;
|
|
7
|
+
temp(name?: string): string;
|
|
8
|
+
}
|
|
9
|
+
export interface SandboxExecResult {
|
|
10
|
+
code: number;
|
|
11
|
+
stdout: string;
|
|
12
|
+
stderr: string;
|
|
13
|
+
}
|
|
14
|
+
export interface Sandbox {
|
|
15
|
+
kind: SandboxKind;
|
|
16
|
+
workDir?: string;
|
|
17
|
+
fs: SandboxFS;
|
|
18
|
+
exec(cmd: string, opts?: {
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
}): Promise<SandboxExecResult>;
|
|
21
|
+
}
|
|
22
|
+
export declare class LocalSandbox implements Sandbox {
|
|
23
|
+
kind: SandboxKind;
|
|
24
|
+
workDir: string;
|
|
25
|
+
fs: SandboxFS;
|
|
26
|
+
constructor(opts: {
|
|
27
|
+
workDir?: string;
|
|
28
|
+
baseDir?: string;
|
|
29
|
+
pwd?: string;
|
|
30
|
+
});
|
|
31
|
+
exec(cmd: string, opts?: {
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
}): Promise<SandboxExecResult>;
|
|
34
|
+
static local(opts: {
|
|
35
|
+
workDir?: string;
|
|
36
|
+
baseDir?: string;
|
|
37
|
+
pwd?: string;
|
|
38
|
+
}): LocalSandbox;
|
|
39
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalSandbox = void 0;
|
|
4
|
+
class LocalSandbox {
|
|
5
|
+
constructor(opts) {
|
|
6
|
+
this.kind = 'local';
|
|
7
|
+
// Support workDir, baseDir (deprecated), and pwd (alias) for backward compatibility
|
|
8
|
+
this.workDir = require('path').resolve(opts.workDir || opts.baseDir || opts.pwd || process.cwd());
|
|
9
|
+
this.fs = new LocalFS(this.workDir);
|
|
10
|
+
}
|
|
11
|
+
async exec(cmd, opts) {
|
|
12
|
+
const { exec } = require('child_process');
|
|
13
|
+
const util = require('util');
|
|
14
|
+
const execPromise = util.promisify(exec);
|
|
15
|
+
const timeout = opts?.timeoutMs || 120000;
|
|
16
|
+
try {
|
|
17
|
+
const { stdout, stderr } = await execPromise(cmd, {
|
|
18
|
+
cwd: this.workDir,
|
|
19
|
+
timeout,
|
|
20
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
21
|
+
});
|
|
22
|
+
return { code: 0, stdout: stdout || '', stderr: stderr || '' };
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
return {
|
|
26
|
+
code: error.code || 1,
|
|
27
|
+
stdout: error.stdout || '',
|
|
28
|
+
stderr: error.stderr || error.message || '',
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
static local(opts) {
|
|
33
|
+
return new LocalSandbox(opts);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.LocalSandbox = LocalSandbox;
|
|
37
|
+
class LocalFS {
|
|
38
|
+
constructor(workDir) {
|
|
39
|
+
this.workDir = workDir;
|
|
40
|
+
}
|
|
41
|
+
resolve(p) {
|
|
42
|
+
const path = require('path');
|
|
43
|
+
if (path.isAbsolute(p))
|
|
44
|
+
return p;
|
|
45
|
+
return path.resolve(this.workDir, p);
|
|
46
|
+
}
|
|
47
|
+
isInside(p) {
|
|
48
|
+
const path = require('path');
|
|
49
|
+
const resolved = this.resolve(p);
|
|
50
|
+
const relative = path.relative(this.workDir, resolved);
|
|
51
|
+
return !relative.startsWith('..') && !path.isAbsolute(relative);
|
|
52
|
+
}
|
|
53
|
+
async read(p) {
|
|
54
|
+
const fs = require('fs').promises;
|
|
55
|
+
const resolved = this.resolve(p);
|
|
56
|
+
if (!this.isInside(resolved)) {
|
|
57
|
+
throw new Error(`Path outside sandbox: ${p}`);
|
|
58
|
+
}
|
|
59
|
+
return await fs.readFile(resolved, 'utf-8');
|
|
60
|
+
}
|
|
61
|
+
async write(p, content) {
|
|
62
|
+
const fs = require('fs').promises;
|
|
63
|
+
const path = require('path');
|
|
64
|
+
const resolved = this.resolve(p);
|
|
65
|
+
if (!this.isInside(resolved)) {
|
|
66
|
+
throw new Error(`Path outside sandbox: ${p}`);
|
|
67
|
+
}
|
|
68
|
+
const dir = path.dirname(resolved);
|
|
69
|
+
await fs.mkdir(dir, { recursive: true });
|
|
70
|
+
await fs.writeFile(resolved, content, 'utf-8');
|
|
71
|
+
}
|
|
72
|
+
temp(name) {
|
|
73
|
+
const path = require('path');
|
|
74
|
+
const tempName = name || `temp-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
75
|
+
return path.relative(this.workDir, path.join(this.workDir, '.temp', tempName));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Message, Timeline, Snapshot, AgentInfo } from '../core/types';
|
|
2
|
+
export interface Store {
|
|
3
|
+
saveMessages(sessionId: string, messages: Message[]): Promise<void>;
|
|
4
|
+
loadMessages(sessionId: string): Promise<Message[]>;
|
|
5
|
+
appendEvent(sessionId: string, timeline: Timeline): Promise<void>;
|
|
6
|
+
readEvents(sessionId: string, since?: number): AsyncIterable<Timeline>;
|
|
7
|
+
saveSnapshot(sessionId: string, snapshot: Snapshot): Promise<void>;
|
|
8
|
+
loadSnapshot(sessionId: string, snapshotId: string): Promise<Snapshot | undefined>;
|
|
9
|
+
listSnapshots(sessionId: string): Promise<Snapshot[]>;
|
|
10
|
+
saveInfo(sessionId: string, info: AgentInfo): Promise<void>;
|
|
11
|
+
loadInfo(sessionId: string): Promise<AgentInfo | undefined>;
|
|
12
|
+
exists(sessionId: string): Promise<boolean>;
|
|
13
|
+
delete(sessionId: string): Promise<void>;
|
|
14
|
+
list(prefix?: string): Promise<string[]>;
|
|
15
|
+
}
|
|
16
|
+
export declare class JSONStore implements Store {
|
|
17
|
+
private baseDir;
|
|
18
|
+
constructor(baseDir: string);
|
|
19
|
+
private getPath;
|
|
20
|
+
saveMessages(sessionId: string, messages: Message[]): Promise<void>;
|
|
21
|
+
loadMessages(sessionId: string): Promise<Message[]>;
|
|
22
|
+
appendEvent(sessionId: string, timeline: Timeline): Promise<void>;
|
|
23
|
+
readEvents(sessionId: string, since?: number): AsyncIterable<Timeline>;
|
|
24
|
+
saveSnapshot(sessionId: string, snapshot: Snapshot): Promise<void>;
|
|
25
|
+
loadSnapshot(sessionId: string, snapshotId: string): Promise<Snapshot | undefined>;
|
|
26
|
+
listSnapshots(sessionId: string): Promise<Snapshot[]>;
|
|
27
|
+
saveInfo(sessionId: string, info: AgentInfo): Promise<void>;
|
|
28
|
+
loadInfo(sessionId: string): Promise<AgentInfo | undefined>;
|
|
29
|
+
exists(sessionId: string): Promise<boolean>;
|
|
30
|
+
delete(sessionId: string): Promise<void>;
|
|
31
|
+
list(prefix?: string): Promise<string[]>;
|
|
32
|
+
}
|