@alpacakit/agents 0.1.0-beta.37 → 0.2.0-beta.2

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 (64) hide show
  1. package/README.md +121 -1
  2. package/dist/agents/claude-session.d.ts +37 -0
  3. package/dist/agents/claude-session.d.ts.map +1 -0
  4. package/dist/agents/claude-session.js +419 -0
  5. package/dist/agents/claude-stream.d.ts +65 -0
  6. package/dist/agents/claude-stream.d.ts.map +1 -0
  7. package/dist/agents/claude-stream.js +111 -0
  8. package/dist/agents/claude.d.ts +2 -1
  9. package/dist/agents/claude.d.ts.map +1 -1
  10. package/dist/agents/claude.js +6 -107
  11. package/dist/agents/codex-session.d.ts +52 -0
  12. package/dist/agents/codex-session.d.ts.map +1 -0
  13. package/dist/agents/codex-session.js +495 -0
  14. package/dist/agents/codex.d.ts +2 -1
  15. package/dist/agents/codex.d.ts.map +1 -1
  16. package/dist/agents/codex.js +3 -1
  17. package/dist/codex/app-server.d.ts +50 -1
  18. package/dist/codex/app-server.d.ts.map +1 -1
  19. package/dist/codex/app-server.js +130 -27
  20. package/dist/codex/index.d.ts +3 -2
  21. package/dist/codex/index.d.ts.map +1 -1
  22. package/dist/codex/index.js +2 -1
  23. package/dist/codex/protocol.d.ts +101 -0
  24. package/dist/codex/protocol.d.ts.map +1 -1
  25. package/dist/codex/protocol.js +78 -0
  26. package/dist/core/process-close.d.ts +11 -0
  27. package/dist/core/process-close.d.ts.map +1 -0
  28. package/dist/core/process-close.js +27 -0
  29. package/dist/core/session.d.ts +100 -0
  30. package/dist/core/session.d.ts.map +1 -0
  31. package/dist/core/session.js +29 -0
  32. package/dist/host/session-files.d.ts +22 -0
  33. package/dist/host/session-files.d.ts.map +1 -0
  34. package/dist/host/session-files.js +91 -0
  35. package/dist/index.d.ts +7 -2
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +3 -0
  38. package/dist/internal/controlled-process/host-controller.d.ts.map +1 -1
  39. package/dist/internal/controlled-process/host-controller.js +23 -1
  40. package/dist/internal/controlled-process/supervisor-runtime.js +41 -2
  41. package/dist/kernel/definition.d.ts +11 -2
  42. package/dist/kernel/definition.d.ts.map +1 -1
  43. package/dist/kernel/facets.d.ts +38 -1
  44. package/dist/kernel/facets.d.ts.map +1 -1
  45. package/dist/kernel/facets.js +20 -0
  46. package/dist/kernel/runtime.d.ts +10 -6
  47. package/dist/kernel/runtime.d.ts.map +1 -1
  48. package/dist/kernel/runtime.js +4 -1
  49. package/dist/kernel/session.d.ts +97 -0
  50. package/dist/kernel/session.d.ts.map +1 -0
  51. package/dist/kernel/session.js +255 -0
  52. package/dist/testing/fake-exec.d.ts +6 -0
  53. package/dist/testing/fake-exec.d.ts.map +1 -1
  54. package/dist/testing/fake-exec.js +3 -1
  55. package/package.json +2 -2
  56. package/dist/auth/profile-store.d.ts +0 -14
  57. package/dist/auth/profile-store.d.ts.map +0 -1
  58. package/dist/auth/profile-store.js +0 -102
  59. package/dist/keyring/index.d.ts +0 -2
  60. package/dist/keyring/index.d.ts.map +0 -1
  61. package/dist/keyring/index.js +0 -1
  62. package/dist/keyring/resolver.d.ts +0 -9
  63. package/dist/keyring/resolver.d.ts.map +0 -1
  64. package/dist/keyring/resolver.js +0 -64
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Claude CLI stream-json vocabulary and line readers, shared by the
3
+ * one-shot run adapter (`claude.ts`) and the long-lived session adapter
4
+ * (`claude-session.ts`): one place spells the CLI's flags and the NDJSON
5
+ * field names both of them read.
6
+ */
7
+ import { readJsonRecord, readNumber, readRecord, readString, readUnknownRecord, } from "../core/json.js";
8
+ import { AGENT_EVENT_TYPE } from "../core/run.js";
9
+ export const CLAUDE = {
10
+ id: "claude",
11
+ command: "claude",
12
+ cli: {
13
+ prompt: "-p",
14
+ model: "--model",
15
+ effort: "--effort",
16
+ outputFormat: "--output-format",
17
+ streamJson: "stream-json",
18
+ permissionMode: "--permission-mode",
19
+ bypassPermissions: "bypassPermissions",
20
+ defaultPermissionMode: "default",
21
+ verbose: "--verbose",
22
+ tools: "--tools",
23
+ allowedTools: "--allowedTools",
24
+ safeMode: "--safe-mode",
25
+ strictMcpConfig: "--strict-mcp-config",
26
+ noSessionPersistence: "--no-session-persistence",
27
+ },
28
+ auth: {
29
+ statusArgs: ["auth", "status", "--json"],
30
+ loginArgs: ["auth", "login", "--claudeai"],
31
+ },
32
+ mcp: {
33
+ addJson: "add-json",
34
+ mcp: "mcp",
35
+ remove: "remove",
36
+ scope: "--scope",
37
+ },
38
+ plugin: {
39
+ plugin: "plugin",
40
+ install: "install",
41
+ },
42
+ json: {
43
+ field: {
44
+ type: "type",
45
+ message: "message",
46
+ content: "content",
47
+ text: "text",
48
+ name: "name",
49
+ usage: "usage",
50
+ result: "result",
51
+ inputTokens: "input_tokens",
52
+ outputTokens: "output_tokens",
53
+ totalCost: "total_cost_usd",
54
+ },
55
+ type: {
56
+ assistant: "assistant",
57
+ result: "result",
58
+ text: "text",
59
+ toolUse: "tool_use",
60
+ },
61
+ },
62
+ };
63
+ export function parseClaudeLine(line) {
64
+ const record = readJsonRecord(line);
65
+ if (record === null ||
66
+ readString(record, CLAUDE.json.field.type) !== CLAUDE.json.type.assistant) {
67
+ return [];
68
+ }
69
+ const message = readRecord(record, CLAUDE.json.field.message);
70
+ const content = message?.[CLAUDE.json.field.content];
71
+ if (!Array.isArray(content)) {
72
+ return [{ type: AGENT_EVENT_TYPE.turn }];
73
+ }
74
+ const events = [{ type: AGENT_EVENT_TYPE.turn }];
75
+ for (const block of content) {
76
+ const blockRecord = readUnknownRecord(block);
77
+ if (blockRecord === null) {
78
+ continue;
79
+ }
80
+ const blockType = readString(blockRecord, CLAUDE.json.field.type);
81
+ if (blockType === CLAUDE.json.type.text) {
82
+ const text = readString(blockRecord, CLAUDE.json.field.text);
83
+ if (text !== null) {
84
+ events.push({ type: AGENT_EVENT_TYPE.message, text });
85
+ }
86
+ }
87
+ if (blockType === CLAUDE.json.type.toolUse) {
88
+ const name = readString(blockRecord, CLAUDE.json.field.name);
89
+ if (name !== null) {
90
+ events.push({ type: AGENT_EVENT_TYPE.tool, name });
91
+ }
92
+ }
93
+ }
94
+ return events;
95
+ }
96
+ export function parseClaudeResult(line) {
97
+ const record = readJsonRecord(line);
98
+ if (record === null ||
99
+ readString(record, CLAUDE.json.field.type) !== CLAUDE.json.type.result) {
100
+ return null;
101
+ }
102
+ const usageRecord = readRecord(record, CLAUDE.json.field.usage);
103
+ const usage = usageRecord === null
104
+ ? null
105
+ : {
106
+ inputTokens: readNumber(usageRecord, CLAUDE.json.field.inputTokens),
107
+ outputTokens: readNumber(usageRecord, CLAUDE.json.field.outputTokens),
108
+ costUsd: readNumber(record, CLAUDE.json.field.totalCost),
109
+ };
110
+ return { text: readString(record, CLAUDE.json.field.result), usage };
111
+ }
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { type FailureMatcher } from "../kernel/classify.js";
9
9
  import type { AgentDefinition } from "../kernel/definition.js";
10
+ import { type ClaudeSessionPermissions } from "./claude-session.js";
10
11
  export declare const CLAUDE_CONFIG_DIR_ENV_VAR = "CLAUDE_CONFIG_DIR";
11
12
  export declare const CLAUDE_API_KEY_ENV_VAR = "ANTHROPIC_API_KEY";
12
13
  /**
@@ -23,5 +24,5 @@ export type ClaudeOptions = {
23
24
  */
24
25
  readonly customizations?: "inherit" | "disabled";
25
26
  };
26
- export declare function claude(options?: ClaudeOptions): AgentDefinition<"claude">;
27
+ export declare function claude(options?: ClaudeOptions): AgentDefinition<"claude", ClaudeSessionPermissions>;
27
28
  //# sourceMappingURL=claude.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"claude.d.ts","sourceRoot":"","sources":["../../src/agents/claude.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA+CH,OAAO,EACL,KAAK,cAAc,EAEpB,MAAM,uBAAuB,CAAC;AAO/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAG/D,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,sBAAsB,sBAAsB,CAAC;AAsK1D;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,cAAc,CA0CvD;AA6TD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,GAAG,UAAU,CAAC;CAClD,CAAC;AAEF,wBAAgB,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,eAAe,CAAC,QAAQ,CAAC,CAgF7E"}
1
+ {"version":3,"file":"claude.d.ts","sourceRoot":"","sources":["../../src/agents/claude.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA4CH,OAAO,EACL,KAAK,cAAc,EAEpB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE/D,OAAO,EACL,KAAK,wBAAwB,EAE9B,MAAM,qBAAqB,CAAC;AAG7B,eAAO,MAAM,yBAAyB,sBAAsB,CAAC;AAC7D,eAAO,MAAM,sBAAsB,sBAAsB,CAAC;AAwD1D;;;;;GAKG;AACH,wBAAgB,sBAAsB,IAAI,cAAc,CA0CvD;AA6TD,MAAM,MAAM,aAAa,GAAG;IAC1B;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,SAAS,GAAG,UAAU,CAAC;CAClD,CAAC;AAEF,wBAAgB,MAAM,CACpB,OAAO,GAAE,aAAkB,GAC1B,eAAe,CAAC,QAAQ,EAAE,wBAAwB,CAAC,CAiFrD"}
@@ -13,8 +13,7 @@ import { AUTH_MATERIAL_KIND, envVarAuthMaterial } from "../auth/material.js";
13
13
  import { requireSecret } from "../auth/secrets.js";
14
14
  import { AGENT_ERROR_CODE, AgentError } from "../core/error.js";
15
15
  import { isErrnoCode } from "../core/fs.js";
16
- import { readBoolean, readJsonRecord, readNumber, readRecord, readString, readUnknownRecord, } from "../core/json.js";
17
- import { AGENT_EVENT_TYPE } from "../core/run.js";
16
+ import { readBoolean, readJsonRecord, readNumber, readRecord, readString, } from "../core/json.js";
18
17
  import { TEXT_ENCODING } from "../core/values.js";
19
18
  import { cliAvailability } from "../host/availability.js";
20
19
  import { INSTALL_STATUS, MCP_TRANSPORT, SKILL_SOURCE_KIND, } from "../host/capabilities.js";
@@ -22,65 +21,13 @@ import { withStagedGithubDirectory, } from "../host/github-dir.js";
22
21
  import { listMcpServersViaCli } from "../host/mcp-list.js";
23
22
  import { hostCommandFailedError, runHostCommand, runHostCommandResult, } from "../host/run-command.js";
24
23
  import { parseEpochTimestamp, } from "../kernel/classify.js";
25
- import { defineCliAgent, } from "../kernel/cli-agent.js";
24
+ import { defineCliAgent } from "../kernel/cli-agent.js";
26
25
  import { cliLoginCommand } from "../kernel/cli-login.js";
27
26
  import { GRANT_KIND, SANDBOX_MODE } from "../kernel/options.js";
27
+ import { claudeSessions, } from "./claude-session.js";
28
+ import { CLAUDE, parseClaudeLine, parseClaudeResult } from "./claude-stream.js";
28
29
  export const CLAUDE_CONFIG_DIR_ENV_VAR = "CLAUDE_CONFIG_DIR";
29
30
  export const CLAUDE_API_KEY_ENV_VAR = "ANTHROPIC_API_KEY";
30
- const CLAUDE = {
31
- id: "claude",
32
- command: "claude",
33
- cli: {
34
- prompt: "-p",
35
- model: "--model",
36
- effort: "--effort",
37
- outputFormat: "--output-format",
38
- streamJson: "stream-json",
39
- permissionMode: "--permission-mode",
40
- bypassPermissions: "bypassPermissions",
41
- defaultPermissionMode: "default",
42
- verbose: "--verbose",
43
- tools: "--tools",
44
- allowedTools: "--allowedTools",
45
- safeMode: "--safe-mode",
46
- strictMcpConfig: "--strict-mcp-config",
47
- noSessionPersistence: "--no-session-persistence",
48
- },
49
- auth: {
50
- statusArgs: ["auth", "status", "--json"],
51
- loginArgs: ["auth", "login", "--claudeai"],
52
- },
53
- mcp: {
54
- addJson: "add-json",
55
- mcp: "mcp",
56
- remove: "remove",
57
- scope: "--scope",
58
- },
59
- plugin: {
60
- plugin: "plugin",
61
- install: "install",
62
- },
63
- json: {
64
- field: {
65
- type: "type",
66
- message: "message",
67
- content: "content",
68
- text: "text",
69
- name: "name",
70
- usage: "usage",
71
- result: "result",
72
- inputTokens: "input_tokens",
73
- outputTokens: "output_tokens",
74
- totalCost: "total_cost_usd",
75
- },
76
- type: {
77
- assistant: "assistant",
78
- result: "result",
79
- text: "text",
80
- toolUse: "tool_use",
81
- },
82
- },
83
- };
84
31
  const COMPETING_TOKEN_VARS = [
85
32
  "ANTHROPIC_API_KEY",
86
33
  "ANTHROPIC_AUTH_TOKEN",
@@ -120,55 +67,6 @@ function sharedArgs(ctx, customizations) {
120
67
  : []),
121
68
  ];
122
69
  }
123
- function parseClaudeLine(line) {
124
- const record = readJsonRecord(line);
125
- if (record === null ||
126
- readString(record, CLAUDE.json.field.type) !== CLAUDE.json.type.assistant) {
127
- return [];
128
- }
129
- const message = readRecord(record, CLAUDE.json.field.message);
130
- const content = message?.[CLAUDE.json.field.content];
131
- if (!Array.isArray(content)) {
132
- return [{ type: AGENT_EVENT_TYPE.turn }];
133
- }
134
- const events = [{ type: AGENT_EVENT_TYPE.turn }];
135
- for (const block of content) {
136
- const blockRecord = readUnknownRecord(block);
137
- if (blockRecord === null) {
138
- continue;
139
- }
140
- const blockType = readString(blockRecord, CLAUDE.json.field.type);
141
- if (blockType === CLAUDE.json.type.text) {
142
- const text = readString(blockRecord, CLAUDE.json.field.text);
143
- if (text !== null) {
144
- events.push({ type: AGENT_EVENT_TYPE.message, text });
145
- }
146
- }
147
- if (blockType === CLAUDE.json.type.toolUse) {
148
- const name = readString(blockRecord, CLAUDE.json.field.name);
149
- if (name !== null) {
150
- events.push({ type: AGENT_EVENT_TYPE.tool, name });
151
- }
152
- }
153
- }
154
- return events;
155
- }
156
- function parseClaudeResult(line) {
157
- const record = readJsonRecord(line);
158
- if (record === null ||
159
- readString(record, CLAUDE.json.field.type) !== CLAUDE.json.type.result) {
160
- return null;
161
- }
162
- const usageRecord = readRecord(record, CLAUDE.json.field.usage);
163
- const usage = usageRecord === null
164
- ? null
165
- : {
166
- inputTokens: readNumber(usageRecord, CLAUDE.json.field.inputTokens),
167
- outputTokens: readNumber(usageRecord, CLAUDE.json.field.outputTokens),
168
- costUsd: readNumber(record, CLAUDE.json.field.totalCost),
169
- };
170
- return { text: readString(record, CLAUDE.json.field.result), usage };
171
- }
172
70
  /**
173
71
  * Structured quota signals in Claude CLI NDJSON output (v1 F2), scanned
174
72
  * per line: rate_limit_event rejected (with resetsAt), api_error_status
@@ -465,7 +363,7 @@ async function listClaudeMcp(ctx) {
465
363
  }
466
364
  export function claude(options = {}) {
467
365
  const customizations = options.customizations ?? "inherit";
468
- return defineCliAgent({
366
+ const definition = defineCliAgent({
469
367
  id: CLAUDE.id,
470
368
  command: CLAUDE.command,
471
369
  args: {
@@ -540,4 +438,5 @@ export function claude(options = {}) {
540
438
  remove: removeClaudeMcp,
541
439
  },
542
440
  });
441
+ return { ...definition, session: claudeSessions({ customizations }) };
543
442
  }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Codex session adapter: one `codex app-server` process holding one thread.
3
+ *
4
+ * Protocol facts (codex 0.153.4, T53 / T57 / T61 lab runs):
5
+ * - `thread/start {cwd, sandbox, approvalPolicy, approvalsReviewer}` creates
6
+ * the thread and takes its writer lock; with a writable sandbox Codex also
7
+ * appends a `trust_level` entry for `cwd` to `~/.codex/config.toml`;
8
+ * - `thread/resume` must repeat the permissions — without `sandbox` the
9
+ * resumed turns ran as danger-full-access; it answers -32600 "already has
10
+ * an active writer" while another process holds the thread and -32600
11
+ * "… is archived" for an archived one (`thread/unarchive` first, opt-in);
12
+ * - `turn/start` answers the turn id at once; `turn/steer {expectedTurnId}`
13
+ * appends to it; `turn/interrupt` ends it with `turn/completed`
14
+ * status `interrupted` (the command's own `item/completed` may trail by ~36 s);
15
+ * - `item/commandExecution/requestApproval` is a server request answered by
16
+ * `{"decision": "accept" | "decline"}`; closing stdin releases the lock.
17
+ */
18
+ import { CODEX_SANDBOX_MODE } from "../codex/protocol.js";
19
+ export type { CodexSandboxMode } from "../codex/protocol.js";
20
+ import { type SessionCapability } from "../kernel/session.js";
21
+ /** `approval_policy` words (codex 0.153.4 `AskForApproval`; the granular object form is not modeled). */
22
+ export declare const CODEX_APPROVAL_POLICY: {
23
+ readonly untrusted: "untrusted";
24
+ readonly onRequest: "on-request";
25
+ readonly never: "never";
26
+ };
27
+ export type CodexApprovalPolicy = (typeof CODEX_APPROVAL_POLICY)[keyof typeof CODEX_APPROVAL_POLICY];
28
+ export declare const CODEX_APPROVALS_REVIEWER: {
29
+ readonly user: "user";
30
+ readonly autoReview: "auto_review";
31
+ readonly guardianSubagent: "guardian_subagent";
32
+ };
33
+ export type CodexApprovalsReviewer = (typeof CODEX_APPROVALS_REVIEWER)[keyof typeof CODEX_APPROVALS_REVIEWER];
34
+ /** Spelled as the rollout's `turn_context.sandbox_policy` records it. */
35
+ export type CodexSandboxPolicy = {
36
+ readonly type: typeof CODEX_SANDBOX_MODE.readOnly;
37
+ } | {
38
+ readonly type: typeof CODEX_SANDBOX_MODE.dangerFullAccess;
39
+ } | {
40
+ readonly type: typeof CODEX_SANDBOX_MODE.workspaceWrite;
41
+ readonly writableRoots: readonly string[];
42
+ readonly networkAccess: boolean;
43
+ readonly excludeTmpdirEnvVar: boolean;
44
+ readonly excludeSlashTmp: boolean;
45
+ };
46
+ export type CodexSessionPermissions = {
47
+ readonly approvalPolicy: CodexApprovalPolicy;
48
+ readonly approvalsReviewer: CodexApprovalsReviewer;
49
+ readonly sandbox: CodexSandboxPolicy;
50
+ };
51
+ export declare function codexSessions(): SessionCapability<CodexSessionPermissions>;
52
+ //# sourceMappingURL=codex-session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codex-session.d.ts","sourceRoot":"","sources":["../../src/agents/codex-session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AASH,OAAO,EAGL,kBAAkB,EAGnB,MAAM,sBAAsB,CAAC;AAE9B,YAAY,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AA2B7D,OAAO,EAIL,KAAK,iBAAiB,EAIvB,MAAM,sBAAsB,CAAC;AAK9B,yGAAyG;AACzG,eAAO,MAAM,qBAAqB;;;;CAIxB,CAAC;AAEX,MAAM,MAAM,mBAAmB,GAC7B,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,OAAO,qBAAqB,CAAC,CAAC;AAErE,eAAO,MAAM,wBAAwB;;;;CAI3B,CAAC;AAEX,MAAM,MAAM,sBAAsB,GAChC,CAAC,OAAO,wBAAwB,CAAC,CAAC,MAAM,OAAO,wBAAwB,CAAC,CAAC;AAE3E,yEAAyE;AACzE,MAAM,MAAM,kBAAkB,GAC1B;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,kBAAkB,CAAC,QAAQ,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,kBAAkB,CAAC,gBAAgB,CAAA;CAAE,GAC7D;IACE,QAAQ,CAAC,IAAI,EAAE,OAAO,kBAAkB,CAAC,cAAc,CAAC;IACxD,QAAQ,CAAC,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1C,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC,QAAQ,CAAC,mBAAmB,EAAE,OAAO,CAAC;IACtC,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;CACnC,CAAC;AAEN,MAAM,MAAM,uBAAuB,GAAG;IACpC,QAAQ,CAAC,cAAc,EAAE,mBAAmB,CAAC;IAC7C,QAAQ,CAAC,iBAAiB,EAAE,sBAAsB,CAAC;IACnD,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;CACtC,CAAC;AAkkBF,wBAAgB,aAAa,IAAI,iBAAiB,CAAC,uBAAuB,CAAC,CAM1E"}