@kobinflow/canvas-agent 0.6.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/LICENSE +22 -0
  2. package/README.md +159 -0
  3. package/agent-instructions.md +13 -0
  4. package/dist/agent/claude.d.ts +3 -0
  5. package/dist/agent/claude.js +54 -0
  6. package/dist/agent/codex-client.d.ts +139 -0
  7. package/dist/agent/codex-client.js +937 -0
  8. package/dist/agent/codex-event-history.d.ts +21 -0
  9. package/dist/agent/codex-event-history.js +163 -0
  10. package/dist/agent/codex-history.d.ts +46 -0
  11. package/dist/agent/codex-history.js +822 -0
  12. package/dist/agent/codex-protocol.d.ts +335 -0
  13. package/dist/agent/codex-protocol.js +1 -0
  14. package/dist/agent/codex.d.ts +178 -0
  15. package/dist/agent/codex.js +509 -0
  16. package/dist/agent/message-metadata.d.ts +51 -0
  17. package/dist/agent/message-metadata.js +378 -0
  18. package/dist/agent/types.d.ts +45 -0
  19. package/dist/agent/types.js +1 -0
  20. package/dist/canvas/operations.d.ts +13 -0
  21. package/dist/canvas/operations.js +167 -0
  22. package/dist/canvas/schemas.d.ts +989 -0
  23. package/dist/canvas/schemas.js +155 -0
  24. package/dist/canvas/session.d.ts +163 -0
  25. package/dist/canvas/session.js +523 -0
  26. package/dist/canvas/tools.d.ts +214 -0
  27. package/dist/canvas/tools.js +27 -0
  28. package/dist/canvas/types.d.ts +34 -0
  29. package/dist/canvas/types.js +1 -0
  30. package/dist/config.d.ts +30 -0
  31. package/dist/config.js +86 -0
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.js +7 -0
  34. package/dist/server/http.d.ts +2 -0
  35. package/dist/server/http.js +576 -0
  36. package/dist/server/mcp.d.ts +2 -0
  37. package/dist/server/mcp.js +27 -0
  38. package/dist/skills/store.d.ts +54 -0
  39. package/dist/skills/store.js +426 -0
  40. package/dist/utils/agent-runtime.d.ts +8 -0
  41. package/dist/utils/agent-runtime.js +33 -0
  42. package/dist/utils/date.d.ts +2 -0
  43. package/dist/utils/date.js +7 -0
  44. package/dist/utils/logger.d.ts +17 -0
  45. package/dist/utils/logger.js +81 -0
  46. package/dist/utils/value.d.ts +5 -0
  47. package/dist/utils/value.js +8 -0
  48. package/dist/version-check.d.ts +2 -0
  49. package/dist/version-check.js +65 -0
  50. package/package.json +55 -0
@@ -0,0 +1,335 @@
1
+ import type { JsonRecord } from "../utils/value.js";
2
+ export type CodexThread = JsonRecord & {
3
+ id: string;
4
+ cwd: string;
5
+ turns?: CodexTurn[];
6
+ };
7
+ export type CodexTurn = JsonRecord & {
8
+ id: string;
9
+ error?: CodexTurnError | null;
10
+ durationMs?: number | null;
11
+ };
12
+ export type CodexTurnError = JsonRecord & {
13
+ message: string;
14
+ };
15
+ export type CodexItem = JsonRecord & {
16
+ id: string;
17
+ type: string;
18
+ text?: string;
19
+ };
20
+ export type CodexPlanStep = {
21
+ step: string;
22
+ status: "pending" | "inProgress" | "completed";
23
+ };
24
+ export type CodexPlanUpdate = {
25
+ threadId: string;
26
+ turnId: string;
27
+ explanation?: string | null;
28
+ plan: CodexPlanStep[];
29
+ turnStatus?: string;
30
+ };
31
+ export type CodexMcpStartupStatus = {
32
+ threadId: string | null;
33
+ name: string;
34
+ status: "starting" | "ready" | "failed" | "cancelled";
35
+ error: string | null;
36
+ failureReason: "reauthenticationRequired" | null;
37
+ };
38
+ export type CodexMcpServerStatus = {
39
+ name: string;
40
+ authStatus: "unsupported" | "notLoggedIn" | "bearerToken" | "oAuth";
41
+ } & JsonRecord;
42
+ export type CodexReasoningEffort = "minimal" | "low" | "medium" | "high" | "xhigh" | "max" | "ultra";
43
+ export type CodexModel = JsonRecord & {
44
+ id: string;
45
+ model: string;
46
+ displayName: string;
47
+ defaultReasoningEffort: CodexReasoningEffort;
48
+ supportedReasoningEfforts: Array<{
49
+ reasoningEffort: CodexReasoningEffort;
50
+ description?: string;
51
+ }>;
52
+ isDefault?: boolean;
53
+ };
54
+ export type CodexSkillScope = "user" | "repo" | "system" | "admin";
55
+ export type CodexSkillInterface = JsonRecord & {
56
+ displayName?: string | null;
57
+ shortDescription?: string | null;
58
+ iconSmall?: string | null;
59
+ iconLarge?: string | null;
60
+ iconSmallUrl?: string | null;
61
+ iconLargeUrl?: string | null;
62
+ brandColor?: string | null;
63
+ defaultPrompt?: string | null;
64
+ };
65
+ export type CodexSkillMetadata = JsonRecord & {
66
+ name: string;
67
+ description: string;
68
+ shortDescription?: string | null;
69
+ interface?: CodexSkillInterface | null;
70
+ dependencies?: JsonRecord | null;
71
+ path: string;
72
+ scope: CodexSkillScope;
73
+ enabled: boolean;
74
+ };
75
+ export type CodexSkillError = {
76
+ path: string;
77
+ message: string;
78
+ };
79
+ export type CodexSkillsListEntry = {
80
+ cwd: string;
81
+ skills: CodexSkillMetadata[];
82
+ errors: CodexSkillError[];
83
+ };
84
+ export type CodexSkillSelector = {
85
+ name: string;
86
+ path: string;
87
+ };
88
+ export type CodexTurnInput = {
89
+ type: "text";
90
+ text: string;
91
+ text_elements: [];
92
+ } | {
93
+ type: "localImage";
94
+ path: string;
95
+ } | ({
96
+ type: "skill";
97
+ } & CodexSkillSelector);
98
+ type ThreadOptions = {
99
+ approvalPolicy: "never" | "on-request";
100
+ sandbox: "read-only" | "workspace-write" | "danger-full-access";
101
+ config: JsonRecord;
102
+ cwd?: string;
103
+ developerInstructions?: string;
104
+ ephemeral?: boolean;
105
+ };
106
+ type CodexRequestSpec = {
107
+ initialize: {
108
+ params: {
109
+ clientInfo: {
110
+ name: string;
111
+ title: string;
112
+ version: string;
113
+ };
114
+ capabilities: {
115
+ experimentalApi: boolean;
116
+ requestAttestation: boolean;
117
+ };
118
+ };
119
+ result: JsonRecord;
120
+ };
121
+ "thread/start": {
122
+ params: ThreadOptions & {
123
+ threadSource: "user";
124
+ };
125
+ result: {
126
+ thread: CodexThread;
127
+ };
128
+ };
129
+ "thread/resume": {
130
+ params: ThreadOptions & {
131
+ threadId: string;
132
+ };
133
+ result: {
134
+ thread: CodexThread;
135
+ };
136
+ };
137
+ "thread/fork": {
138
+ params: ThreadOptions & {
139
+ threadId: string;
140
+ threadSource: "user";
141
+ };
142
+ result: {
143
+ thread: CodexThread;
144
+ };
145
+ };
146
+ "thread/list": {
147
+ params: {
148
+ limit: number;
149
+ sortKey: "updated_at";
150
+ sortDirection: "desc";
151
+ sourceKinds: Array<"cli" | "vscode" | "appServer" | "exec">;
152
+ cwd: string;
153
+ searchTerm?: string;
154
+ };
155
+ result: {
156
+ data: CodexThread[];
157
+ nextCursor: string | null;
158
+ backwardsCursor: string | null;
159
+ };
160
+ };
161
+ "thread/read": {
162
+ params: {
163
+ threadId: string;
164
+ includeTurns: boolean;
165
+ };
166
+ result: {
167
+ thread: CodexThread;
168
+ };
169
+ };
170
+ "thread/archive": {
171
+ params: {
172
+ threadId: string;
173
+ };
174
+ result: Record<string, never>;
175
+ };
176
+ "thread/unsubscribe": {
177
+ params: {
178
+ threadId: string;
179
+ };
180
+ result: {
181
+ status: "notLoaded" | "notSubscribed" | "unsubscribed";
182
+ };
183
+ };
184
+ "model/list": {
185
+ params: {
186
+ limit: number;
187
+ includeHidden: boolean;
188
+ };
189
+ result: {
190
+ data: CodexModel[];
191
+ nextCursor: string | null;
192
+ };
193
+ };
194
+ "mcpServerStatus/list": {
195
+ params: {
196
+ cursor?: string | null;
197
+ limit?: number | null;
198
+ detail?: "full" | "toolsAndAuthOnly" | null;
199
+ threadId?: string | null;
200
+ };
201
+ result: {
202
+ data: CodexMcpServerStatus[];
203
+ nextCursor: string | null;
204
+ };
205
+ };
206
+ "skills/list": {
207
+ params: {
208
+ cwds: string[];
209
+ forceReload?: boolean;
210
+ };
211
+ result: {
212
+ data: CodexSkillsListEntry[];
213
+ };
214
+ };
215
+ "skills/config/write": {
216
+ params: {
217
+ path?: string | null;
218
+ name?: string | null;
219
+ enabled: boolean;
220
+ };
221
+ result: {
222
+ effectiveEnabled: boolean;
223
+ };
224
+ };
225
+ "turn/start": {
226
+ params: {
227
+ threadId: string;
228
+ input: CodexTurnInput[];
229
+ approvalPolicy: "never" | "on-request";
230
+ sandboxPolicy: {
231
+ type: "readOnly";
232
+ networkAccess: boolean;
233
+ } | {
234
+ type: "workspaceWrite";
235
+ networkAccess: boolean;
236
+ } | {
237
+ type: "dangerFullAccess";
238
+ };
239
+ model?: string;
240
+ effort?: CodexReasoningEffort;
241
+ outputSchema?: JsonRecord;
242
+ };
243
+ result: {
244
+ turn: CodexTurn;
245
+ };
246
+ };
247
+ "turn/interrupt": {
248
+ params: {
249
+ threadId: string;
250
+ turnId: string;
251
+ };
252
+ result: Record<string, never>;
253
+ };
254
+ };
255
+ export type CodexRequestMethod = keyof CodexRequestSpec;
256
+ export type CodexRequestParams<Method extends CodexRequestMethod> = CodexRequestSpec[Method]["params"];
257
+ export type CodexRequestResult<Method extends CodexRequestMethod> = CodexRequestSpec[Method]["result"];
258
+ type TokenUsageBreakdown = {
259
+ inputTokens: number;
260
+ cachedInputTokens: number;
261
+ outputTokens: number;
262
+ reasoningOutputTokens: number;
263
+ };
264
+ type CodexNotificationSpec = {
265
+ "thread/started": {
266
+ thread: CodexThread;
267
+ };
268
+ "turn/started": {
269
+ threadId?: string;
270
+ turn: CodexTurn;
271
+ };
272
+ "turn/completed": {
273
+ threadId?: string;
274
+ turn: CodexTurn;
275
+ };
276
+ "turn/plan/updated": {
277
+ threadId?: string;
278
+ turnId: string;
279
+ explanation?: string | null;
280
+ plan: CodexPlanStep[];
281
+ };
282
+ "item/started": {
283
+ threadId: string;
284
+ turnId: string;
285
+ item: CodexItem;
286
+ };
287
+ "item/completed": {
288
+ threadId: string;
289
+ turnId: string;
290
+ item: CodexItem;
291
+ };
292
+ "item/agentMessage/delta": {
293
+ threadId: string;
294
+ turnId: string;
295
+ itemId: string;
296
+ delta: string;
297
+ };
298
+ "item/plan/delta": {
299
+ threadId: string;
300
+ turnId: string;
301
+ itemId: string;
302
+ delta: string;
303
+ };
304
+ "item/reasoning/summaryTextDelta": {
305
+ threadId: string;
306
+ turnId: string;
307
+ itemId: string;
308
+ delta: string;
309
+ summaryIndex: number;
310
+ };
311
+ "item/commandExecution/outputDelta": {
312
+ threadId: string;
313
+ turnId: string;
314
+ itemId: string;
315
+ delta: string;
316
+ };
317
+ "thread/tokenUsage/updated": {
318
+ threadId: string;
319
+ turnId: string;
320
+ tokenUsage: {
321
+ last: TokenUsageBreakdown;
322
+ };
323
+ };
324
+ "mcpServer/startupStatus/updated": CodexMcpStartupStatus;
325
+ "skills/changed": Record<string, never>;
326
+ error: {
327
+ threadId: string;
328
+ turnId: string;
329
+ error: CodexTurnError;
330
+ willRetry: boolean;
331
+ };
332
+ };
333
+ export type CodexNotificationMethod = keyof CodexNotificationSpec;
334
+ export type CodexNotificationParams<Method extends CodexNotificationMethod> = CodexNotificationSpec[Method];
335
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,178 @@
1
+ import { z } from "zod";
2
+ import type { CanvasSnapshot } from "../canvas/types.js";
3
+ import { type JsonRecord } from "../utils/value.js";
4
+ import type { CodexReasoningEffort, CodexSkillMetadata, CodexSkillSelector, CodexSkillsListEntry } from "./codex-protocol.js";
5
+ import type { AgentAttachment, AgentEmit, AgentPermissionMode } from "./types.js";
6
+ type CodexRunOptions = {
7
+ threadId?: string;
8
+ cwd?: string;
9
+ permissionMode?: AgentPermissionMode;
10
+ model?: string;
11
+ effort?: CodexReasoningEffort;
12
+ skill?: CodexSkillSelector;
13
+ messageText?: string;
14
+ appEmit?: AgentEmit;
15
+ onStart?: () => void;
16
+ onThread?: (threadId: string) => void;
17
+ onTurn?: (turnId: string) => void;
18
+ onFinish?: () => void;
19
+ };
20
+ type CodexSkillDraftInput = {
21
+ model?: string;
22
+ effort?: CodexReasoningEffort;
23
+ } & ({
24
+ source: "conversation";
25
+ threadId: string;
26
+ } | {
27
+ source: "canvas";
28
+ snapshot: CanvasSnapshot;
29
+ });
30
+ declare const skillDraftSchema: z.ZodEffects<z.ZodObject<{
31
+ name: z.ZodString;
32
+ displayName: z.ZodString;
33
+ description: z.ZodEffects<z.ZodString, string, string>;
34
+ instructions: z.ZodString;
35
+ shortDescription: z.ZodEffects<z.ZodString, string, string>;
36
+ defaultPrompt: z.ZodString;
37
+ }, "strict", z.ZodTypeAny, {
38
+ name: string;
39
+ displayName: string;
40
+ description: string;
41
+ instructions: string;
42
+ shortDescription: string;
43
+ defaultPrompt: string;
44
+ }, {
45
+ name: string;
46
+ displayName: string;
47
+ description: string;
48
+ instructions: string;
49
+ shortDescription: string;
50
+ defaultPrompt: string;
51
+ }>, {
52
+ name: string;
53
+ displayName: string;
54
+ description: string;
55
+ instructions: string;
56
+ shortDescription: string;
57
+ defaultPrompt: string;
58
+ }, {
59
+ name: string;
60
+ displayName: string;
61
+ description: string;
62
+ instructions: string;
63
+ shortDescription: string;
64
+ defaultPrompt: string;
65
+ }>;
66
+ export type AgentSkillDraft = z.infer<typeof skillDraftSchema>;
67
+ export declare class CodexSkillLookupError extends Error {
68
+ readonly statusCode: 400 | 404 | 409;
69
+ name: string;
70
+ constructor(message: string, statusCode: 400 | 404 | 409);
71
+ }
72
+ export { summarizeCodexThread } from "./codex-history.js";
73
+ /** 将 Codex turn 加入串行队列并等待执行完成。 */
74
+ export declare function runCodexTurn(prompt: string, lifecycleEmit: AgentEmit, attachments?: AgentAttachment[], options?: CodexRunOptions): Promise<void>;
75
+ /** 从当前对话或指定网页画布生成可编辑草稿,不写入 Skill 文件。 */
76
+ export declare function generateCodexSkillDraft(emit: AgentEmit, cwd: string, input: CodexSkillDraftInput): Promise<AgentSkillDraft>;
77
+ /** 中断当前线程正在执行的 Codex turn。 */
78
+ export declare function interruptCodexTurn(threadId?: string): Promise<boolean>;
79
+ /** 回复当前 app-server 的待处理权限请求。 */
80
+ export declare function resolveCodexApproval(requestId: string, decision: string): Promise<boolean>;
81
+ /** 创建新的 Codex 线程并记录当前线程 ID。 */
82
+ export declare function startCodexThread(emit: AgentEmit, cwd?: string, permissionMode?: AgentPermissionMode, preheat?: boolean): Promise<import("./codex-protocol.js").CodexThread>;
83
+ /** 恢复指定 Codex 线程并返回聊天历史。 */
84
+ export declare function resumeCodexThread(emit: AgentEmit, threadId: string, cwd?: string, permissionMode?: AgentPermissionMode, preheat?: boolean): Promise<{
85
+ thread: import("./codex-protocol.js").CodexThread;
86
+ messages: {
87
+ id: string;
88
+ itemId: string;
89
+ threadId: string;
90
+ turnId: string;
91
+ role: "user" | "assistant" | "tool" | "error";
92
+ title?: string;
93
+ text: string;
94
+ detail?: unknown;
95
+ activityItems?: Record<string, string>;
96
+ }[];
97
+ settledTurnIds: string[];
98
+ historyReady: boolean;
99
+ }>;
100
+ /** 查询当前工作空间中的 Codex 线程。 */
101
+ export declare function listCodexThreads(emit: AgentEmit, options: {
102
+ cwd: string;
103
+ searchTerm?: string;
104
+ limit?: number;
105
+ }): Promise<{
106
+ data: {
107
+ id: string;
108
+ sessionId: string;
109
+ preview: string;
110
+ name: string | null;
111
+ cwd: string;
112
+ status: string;
113
+ source: unknown;
114
+ threadSource: unknown;
115
+ createdAt: number;
116
+ updatedAt: number;
117
+ }[];
118
+ nextCursor: {} | null;
119
+ backwardsCursor: {} | null;
120
+ }>;
121
+ /** 查询当前账号可用于新任务的 Codex 模型。 */
122
+ export declare function listCodexModels(emit: AgentEmit): Promise<{
123
+ data: import("./codex-protocol.js").CodexModel[];
124
+ nextCursor: string | null;
125
+ }>;
126
+ /** 查询当前工作空间的原生 Skill 列表。 */
127
+ export declare function listCodexSkills(emit: AgentEmit, cwd: string, forceReload?: boolean): Promise<CodexSkillsListEntry>;
128
+ /** 从原生 Skill 列表中解析并校验浏览器提交的选择器。 */
129
+ export declare function resolveCodexSkill(emit: AgentEmit, cwd: string, selector: CodexSkillSelector, requireEnabled?: boolean): Promise<CodexSkillMetadata>;
130
+ /** 修改经过原生列表校验的 Skill 启用状态。 */
131
+ export declare function configureCodexSkill(emit: AgentEmit, cwd: string, selector: CodexSkillSelector, enabled: boolean): Promise<{
132
+ skill: {
133
+ enabled: boolean;
134
+ name: string;
135
+ description: string;
136
+ shortDescription?: string | null;
137
+ interface?: import("./codex-protocol.js").CodexSkillInterface | null;
138
+ dependencies?: JsonRecord | null;
139
+ path: string;
140
+ scope: import("./codex-protocol.js").CodexSkillScope;
141
+ };
142
+ effectiveEnabled: boolean;
143
+ }>;
144
+ /** 读取指定 Codex 线程及其聊天历史。 */
145
+ export declare function readCodexThread(emit: AgentEmit, threadId: string, cwd?: string): Promise<{
146
+ thread: {
147
+ id: string;
148
+ sessionId: string;
149
+ preview: string;
150
+ name: string | null;
151
+ cwd: string;
152
+ status: string;
153
+ source: unknown;
154
+ threadSource: unknown;
155
+ createdAt: number;
156
+ updatedAt: number;
157
+ };
158
+ messages: {
159
+ id: string;
160
+ itemId: string;
161
+ threadId: string;
162
+ turnId: string;
163
+ role: "user" | "assistant" | "tool" | "error";
164
+ title?: string;
165
+ text: string;
166
+ detail?: unknown;
167
+ activityItems?: Record<string, string>;
168
+ }[];
169
+ settledTurnIds: string[];
170
+ historyReady: boolean;
171
+ }>;
172
+ /** 归档指定 Codex 线程。 */
173
+ export declare function archiveCodexThread(emit: AgentEmit, threadId: string, cwd?: string): Promise<void>;
174
+ /** 判断线程异常是否允许自动新建线程后重试。 */
175
+ export declare function isRecoverableThreadError(error: unknown): boolean;
176
+ /** 只保留理解画布流程所需的信息,避免把媒体、外部地址、坐标和本地凭证送入草稿线程。 */
177
+ export declare function canvasSkillSource(snapshot: CanvasSnapshot): JsonRecord;
178
+ export declare function assertDraftHasNoSensitiveValues(draft: AgentSkillDraft, privateValues: string[]): void;