pi-agy-acp 0.2.6 → 0.2.8
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/CHANGELOG.md +14 -0
- package/docs/architecture.md +41 -22
- package/docs/development.md +3 -3
- package/index.ts +65 -45
- package/package.json +4 -4
- package/src/acp-process.ts +13 -27
- package/src/catalog.ts +59 -33
- package/src/config.ts +3 -5
- package/src/conversation.ts +172 -175
- package/src/format.ts +9 -355
- package/src/history.ts +142 -33
- package/src/host.ts +2 -2
- package/src/mcp-bridge.ts +53 -12
- package/src/pi-stream.ts +5 -8
- package/src/preflight.ts +1 -3
- package/src/protocol.ts +18 -3
- package/src/quota.ts +377 -41
- package/src/runtime.ts +321 -235
- package/src/session-db.ts +24 -5
- package/src/util.ts +5 -0
package/src/mcp-bridge.ts
CHANGED
|
@@ -18,10 +18,18 @@ import {
|
|
|
18
18
|
} from 'node:http'
|
|
19
19
|
import type { AddressInfo } from 'node:net'
|
|
20
20
|
import type { ToolSchema } from './protocol.ts'
|
|
21
|
-
import {
|
|
22
|
-
import { digest } from './util.ts'
|
|
21
|
+
import { digest, isRecord } from './util.ts'
|
|
23
22
|
import { VERSION } from './version.ts'
|
|
24
23
|
|
|
24
|
+
/** Server name under which Pi tools reach the agent; the agent prefixes tool names with `mcp_<server>_`. */
|
|
25
|
+
export const MCP_SERVER_NAME = 'pi'
|
|
26
|
+
/** Agent-visible prefix of every Pi tool. */
|
|
27
|
+
export const AGENT_TOOL_PREFIX = `mcp_${MCP_SERVER_NAME}_`
|
|
28
|
+
/** Fallback MCP tool name for invoking any Pi tool omitted from direct declarations. */
|
|
29
|
+
export const CALL_TOOL_NAME = 'pi_call_tool'
|
|
30
|
+
/** Tool the agent calls to keep waiting on a relayed Pi tool result. */
|
|
31
|
+
export const AWAIT_TOOL_NAME = 'pi_await_result'
|
|
32
|
+
|
|
25
33
|
/** One tool schema advertised over MCP. */
|
|
26
34
|
export interface BridgeTool {
|
|
27
35
|
name: string
|
|
@@ -29,6 +37,44 @@ export interface BridgeTool {
|
|
|
29
37
|
inputSchema: Record<string, unknown>
|
|
30
38
|
}
|
|
31
39
|
|
|
40
|
+
/** MCP descriptor of {@link AWAIT_TOOL_NAME}. */
|
|
41
|
+
export const AWAIT_TOOL: BridgeTool = {
|
|
42
|
+
name: AWAIT_TOOL_NAME,
|
|
43
|
+
description:
|
|
44
|
+
'Wait for the result of a harness tool call that reported it is still running. Call this only when a tool result tells you to, passing the call_id it gave.',
|
|
45
|
+
inputSchema: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
call_id: {
|
|
49
|
+
type: 'string',
|
|
50
|
+
description: 'The call id from the still-running notice.',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
required: ['call_id'],
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** MCP descriptor of {@link CALL_TOOL_NAME} (~340B, stays below agy's lazy cutoff). */
|
|
58
|
+
export const CALL_TOOL: BridgeTool = {
|
|
59
|
+
name: CALL_TOOL_NAME,
|
|
60
|
+
description:
|
|
61
|
+
'Invoke any harness tool by exact name when mcp_pi_<name> is absent from your direct tool declarations.',
|
|
62
|
+
inputSchema: {
|
|
63
|
+
type: 'object',
|
|
64
|
+
properties: {
|
|
65
|
+
name: {
|
|
66
|
+
type: 'string',
|
|
67
|
+
description: 'Exact Pi tool name without mcp_pi_ prefix.',
|
|
68
|
+
},
|
|
69
|
+
arguments: {
|
|
70
|
+
type: 'object',
|
|
71
|
+
description: 'Arguments object matching that tool schema.',
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
required: ['name'],
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
|
|
32
78
|
/** One MCP content item returned to the agent. */
|
|
33
79
|
export type BridgeContent =
|
|
34
80
|
| { type: 'text'; text: string }
|
|
@@ -45,7 +91,7 @@ export interface BridgeCallRequest {
|
|
|
45
91
|
/** Tenant key taken from the request path, when the session registered one. */
|
|
46
92
|
tenantKey: string | undefined
|
|
47
93
|
/** ACP session id the agent reported in `_meta`, when present. */
|
|
48
|
-
|
|
94
|
+
acpSessionId: string | undefined
|
|
49
95
|
/** MCP tool name without any agent-side prefix. */
|
|
50
96
|
name: string
|
|
51
97
|
/** Parsed tool arguments. */
|
|
@@ -355,7 +401,7 @@ export class McpBridge {
|
|
|
355
401
|
const name = typeof params.name === 'string' ? params.name : ''
|
|
356
402
|
const args = isRecord(params.arguments) ? params.arguments : {}
|
|
357
403
|
const progressToken = meta.progressToken
|
|
358
|
-
const
|
|
404
|
+
const acpSessionId =
|
|
359
405
|
typeof meta[CONVERSATION_META_KEY] === 'string'
|
|
360
406
|
? (meta[CONVERSATION_META_KEY] as string)
|
|
361
407
|
: undefined
|
|
@@ -428,7 +474,7 @@ export class McpBridge {
|
|
|
428
474
|
try {
|
|
429
475
|
result = await this.router.callTool({
|
|
430
476
|
tenantKey,
|
|
431
|
-
|
|
477
|
+
acpSessionId,
|
|
432
478
|
name,
|
|
433
479
|
arguments: args,
|
|
434
480
|
signal: controller.signal,
|
|
@@ -470,10 +516,6 @@ interface RpcRequest {
|
|
|
470
516
|
params?: unknown
|
|
471
517
|
}
|
|
472
518
|
|
|
473
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
474
|
-
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
475
|
-
}
|
|
476
|
-
|
|
477
519
|
class HttpError extends Error {
|
|
478
520
|
readonly status: number
|
|
479
521
|
|
|
@@ -573,10 +615,9 @@ function shortenText(text: string, maxChars: number): string {
|
|
|
573
615
|
function compactSchemaNode(value: unknown, maxDescChars: number): unknown {
|
|
574
616
|
if (Array.isArray(value))
|
|
575
617
|
return value.map((item) => compactSchemaNode(item, maxDescChars))
|
|
576
|
-
if (value
|
|
577
|
-
const record = value as Record<string, unknown>
|
|
618
|
+
if (!isRecord(value)) return value
|
|
578
619
|
const out: Record<string, unknown> = {}
|
|
579
|
-
for (const [key, entry] of Object.entries(
|
|
620
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
580
621
|
if (key === 'description' && typeof entry === 'string') {
|
|
581
622
|
if (maxDescChars > 0) out[key] = shortenText(entry, maxDescChars)
|
|
582
623
|
continue
|
package/src/pi-stream.ts
CHANGED
|
@@ -17,11 +17,12 @@ import type {
|
|
|
17
17
|
import type {
|
|
18
18
|
ContentBlock,
|
|
19
19
|
GenerateOptions,
|
|
20
|
+
LlmErrorCode,
|
|
20
21
|
RequestMessage,
|
|
21
22
|
StreamChunk,
|
|
22
23
|
} from './protocol.ts'
|
|
23
24
|
import { LlmError } from './protocol.ts'
|
|
24
|
-
import { digest } from './util.ts'
|
|
25
|
+
import { digest, isRecord } from './util.ts'
|
|
25
26
|
|
|
26
27
|
export function requestMessages(messages: Message[]): RequestMessage[] {
|
|
27
28
|
let leadingSystem = true
|
|
@@ -83,7 +84,7 @@ export function requestMessages(messages: Message[]): RequestMessage[] {
|
|
|
83
84
|
|
|
84
85
|
function errorMessage(
|
|
85
86
|
message: string | undefined,
|
|
86
|
-
code?:
|
|
87
|
+
code?: LlmErrorCode,
|
|
87
88
|
): string | undefined {
|
|
88
89
|
if (
|
|
89
90
|
code === 'CONTEXT_WINDOW_EXCEEDED' &&
|
|
@@ -151,17 +152,13 @@ export function streamPi(
|
|
|
151
152
|
// the original tools; passing `tools: []` explicitly disables them.
|
|
152
153
|
const replacement = await options.onPayload?.(request, model)
|
|
153
154
|
if (replacement !== undefined) {
|
|
154
|
-
if (
|
|
155
|
-
replacement === null ||
|
|
156
|
-
typeof replacement !== 'object' ||
|
|
157
|
-
Array.isArray(replacement)
|
|
158
|
-
) {
|
|
155
|
+
if (!isRecord(replacement)) {
|
|
159
156
|
throw new LlmError(
|
|
160
157
|
'onPayload must return a payload object or undefined',
|
|
161
158
|
'INVALID_PAYLOAD',
|
|
162
159
|
)
|
|
163
160
|
}
|
|
164
|
-
const candidate = replacement
|
|
161
|
+
const candidate = replacement
|
|
165
162
|
if (
|
|
166
163
|
typeof candidate.model !== 'string' ||
|
|
167
164
|
candidate.model.trim().length === 0
|
package/src/preflight.ts
CHANGED
|
@@ -29,8 +29,6 @@ import { readJson } from './config.ts'
|
|
|
29
29
|
import { removeArtifacts } from './session-db.ts'
|
|
30
30
|
import { atomicWriteFile } from './util.ts'
|
|
31
31
|
|
|
32
|
-
export { atomicWriteFile }
|
|
33
|
-
|
|
34
32
|
/** Agent id installed in the isolated home. */
|
|
35
33
|
export const BRIDGE_AGENT = 'pi-bridge'
|
|
36
34
|
/** Minimum official CLI version supported (`agy 1.5.23+`). */
|
|
@@ -496,7 +494,7 @@ export function prepareHome(
|
|
|
496
494
|
syncOAuthToken(home, userHome, env)
|
|
497
495
|
} catch (error) {
|
|
498
496
|
onWarn?.(
|
|
499
|
-
`
|
|
497
|
+
`OAuth token sync skipped: ${error instanceof Error ? error.message : String(error)}`,
|
|
500
498
|
)
|
|
501
499
|
}
|
|
502
500
|
const settingsPath = join(appData, 'settings.json')
|
package/src/protocol.ts
CHANGED
|
@@ -33,6 +33,21 @@ export interface TokenUsage {
|
|
|
33
33
|
reasoningTokens?: number
|
|
34
34
|
totalTokens: number
|
|
35
35
|
}
|
|
36
|
+
export type LlmErrorCode =
|
|
37
|
+
| 'ABORTED'
|
|
38
|
+
| 'AUTH'
|
|
39
|
+
| 'CONTEXT_WINDOW_EXCEEDED'
|
|
40
|
+
| 'EMPTY_RESPONSE'
|
|
41
|
+
| 'INVALID_PAYLOAD'
|
|
42
|
+
| 'INVALID_REQUEST'
|
|
43
|
+
| 'PROVIDER_ERROR'
|
|
44
|
+
| 'QUOTA'
|
|
45
|
+
| 'RATE_LIMIT'
|
|
46
|
+
| 'REFUSAL'
|
|
47
|
+
| 'SERVER'
|
|
48
|
+
| 'TIMEOUT'
|
|
49
|
+
| 'TRANSPORT'
|
|
50
|
+
| 'UNKNOWN_MODEL'
|
|
36
51
|
export type StreamChunk =
|
|
37
52
|
| {
|
|
38
53
|
type: 'block-start'
|
|
@@ -53,12 +68,12 @@ export type StreamChunk =
|
|
|
53
68
|
type: 'finish'
|
|
54
69
|
reason: {
|
|
55
70
|
kind: 'stop' | 'tool-calls' | 'max-tokens' | 'aborted' | 'error'
|
|
56
|
-
failure?: { message: string; code:
|
|
71
|
+
failure?: { message: string; code: LlmErrorCode }
|
|
57
72
|
}
|
|
58
73
|
}
|
|
59
74
|
export class LlmError extends Error {
|
|
60
|
-
readonly code:
|
|
61
|
-
constructor(message: string, code:
|
|
75
|
+
readonly code: LlmErrorCode
|
|
76
|
+
constructor(message: string, code: LlmErrorCode, options?: ErrorOptions) {
|
|
62
77
|
super(message, options)
|
|
63
78
|
this.code = code
|
|
64
79
|
}
|