@agimon-ai/po-approval 0.0.0-alpha.100
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/LICENSE +149 -0
- package/README.md +67 -0
- package/dist/apiContracts.cjs +58 -0
- package/dist/apiContracts.cjs.map +1 -0
- package/dist/apiContracts.d.mts +53 -0
- package/dist/apiContracts.d.mts.map +1 -0
- package/dist/apiContracts.mjs +53 -0
- package/dist/apiContracts.mjs.map +1 -0
- package/dist/approvalSession-BAbRvyI-.cjs +144 -0
- package/dist/approvalSession-BAbRvyI-.cjs.map +1 -0
- package/dist/approvalSession-Bs554RFc.mjs +127 -0
- package/dist/approvalSession-Bs554RFc.mjs.map +1 -0
- package/dist/extensions/native.mjs +719 -0
- package/dist/extensions/native.mjs.map +1 -0
- package/dist/extensions/server.cjs +909 -0
- package/dist/extensions/server.cjs.map +1 -0
- package/dist/extensions/server.d.mts +5 -0
- package/dist/extensions/server.d.mts.map +1 -0
- package/dist/extensions/server.mjs +881 -0
- package/dist/extensions/server.mjs.map +1 -0
- package/dist/extensions/web.mjs +607 -0
- package/dist/extensions/web.mjs.map +1 -0
- package/dist/index.cjs +4 -0
- package/dist/index.d.mts +126 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1 -0
- package/llms.txt +7 -0
- package/package.json +153 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { defineServiceContract } from "@agimon-ai/po-core/serverPlugin";
|
|
2
|
+
//#region src/services/approvalRoot/type.ts
|
|
3
|
+
var ApprovalRequestError = class extends Error {
|
|
4
|
+
status;
|
|
5
|
+
constructor(message, status = 409) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.name = "ApprovalRequestError";
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const APPROVAL_ROOT_SERVICE = defineServiceContract("po/approval-root", { scope: "global" });
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/services/approvalSession/index.ts
|
|
14
|
+
const OUTCOME_MESSAGE_TYPE = "approval-outcome";
|
|
15
|
+
function prompt(record) {
|
|
16
|
+
const { title, summary, details } = record.description;
|
|
17
|
+
return {
|
|
18
|
+
id: record.id,
|
|
19
|
+
toolCallId: record.toolCallId,
|
|
20
|
+
operation: record.operation,
|
|
21
|
+
requirement: record.requirement,
|
|
22
|
+
title,
|
|
23
|
+
summary,
|
|
24
|
+
...details === void 0 ? {} : { details }
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** One session's view of the global queue: it submits only its own calls and runs only its own approved ones. */
|
|
28
|
+
function createApprovalSession(options) {
|
|
29
|
+
const { root, sessionId } = options;
|
|
30
|
+
const executors = /* @__PURE__ */ new Map();
|
|
31
|
+
const lifetime = new AbortController();
|
|
32
|
+
const binding = {
|
|
33
|
+
signal: lifetime.signal,
|
|
34
|
+
ready: (kind) => executors.has(kind),
|
|
35
|
+
run: (record, signal) => {
|
|
36
|
+
const executor = executors.get(record.operation.kind);
|
|
37
|
+
if (executor === void 0) throw new Error(`Nothing in this session can run ${record.operation.name}.`);
|
|
38
|
+
return executor({
|
|
39
|
+
approvalId: record.id,
|
|
40
|
+
operation: record.operation,
|
|
41
|
+
input: record.input,
|
|
42
|
+
...record.argumentFormat === void 0 ? {} : { argumentFormat: record.argumentFormat }
|
|
43
|
+
}, signal);
|
|
44
|
+
},
|
|
45
|
+
deliver: async (record, text) => {
|
|
46
|
+
if (options.session?.admitMessage === void 0) throw new Error("This session cannot receive messages.");
|
|
47
|
+
await options.session.admitMessage({
|
|
48
|
+
type: OUTCOME_MESSAGE_TYPE,
|
|
49
|
+
text,
|
|
50
|
+
details: {
|
|
51
|
+
approvalId: record.id,
|
|
52
|
+
state: record.state
|
|
53
|
+
}
|
|
54
|
+
}, "steer", `approval:${record.id}:${record.state}`);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
let detach;
|
|
58
|
+
const voiceTarget = async () => {
|
|
59
|
+
const pending = (await root.open(sessionId)).filter((record) => record.state === "pending");
|
|
60
|
+
return pending.length === 1 && pending[0].requirement === "ask" ? pending[0] : void 0;
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
start() {
|
|
64
|
+
if (!lifetime.signal.aborted && detach === void 0) detach = root.attach(sessionId, binding);
|
|
65
|
+
},
|
|
66
|
+
service: {
|
|
67
|
+
async submit(request) {
|
|
68
|
+
if (request.sessionId !== sessionId) throw new Error("A session can only ask about its own calls.");
|
|
69
|
+
return root.submit(request);
|
|
70
|
+
},
|
|
71
|
+
registerExecutor(kind, run) {
|
|
72
|
+
executors.set(kind, run);
|
|
73
|
+
root.drain(sessionId);
|
|
74
|
+
return () => {
|
|
75
|
+
if (executors.get(kind) === run) executors.delete(kind);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
async voiceCurrent() {
|
|
80
|
+
const record = await voiceTarget();
|
|
81
|
+
return record === void 0 ? void 0 : prompt(record);
|
|
82
|
+
},
|
|
83
|
+
async voiceAnswer(approvalId, decision) {
|
|
84
|
+
const record = await voiceTarget();
|
|
85
|
+
if (record?.id !== approvalId) return false;
|
|
86
|
+
try {
|
|
87
|
+
await root.decide(record.id, record.revision, decision === "approved" ? "approve" : "deny");
|
|
88
|
+
return true;
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
withdraw: (id) => root.withdraw(sessionId, id),
|
|
94
|
+
async cards() {
|
|
95
|
+
return (await root.open(sessionId)).map((record) => ({
|
|
96
|
+
...prompt(record),
|
|
97
|
+
revision: record.revision,
|
|
98
|
+
state: record.state,
|
|
99
|
+
...record.sessionGone === true ? { sessionGone: true } : {},
|
|
100
|
+
...record.sessionName === void 0 ? {} : { sessionName: record.sessionName.slice(0, 200) },
|
|
101
|
+
...record.workspaceName === void 0 ? {} : { workspaceName: record.workspaceName.slice(0, 200) },
|
|
102
|
+
...record.reason === void 0 ? {} : { reason: record.reason }
|
|
103
|
+
}));
|
|
104
|
+
},
|
|
105
|
+
onChanged(listener) {
|
|
106
|
+
return root.onChanged((changed) => {
|
|
107
|
+
if (changed === sessionId) listener();
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
async decide(id, revision, decision) {
|
|
111
|
+
if (!(await root.open(sessionId)).some((record) => record.id === id)) throw new ApprovalRequestError("This approval is no longer here.", 404);
|
|
112
|
+
await root.decide(id, revision, decision);
|
|
113
|
+
},
|
|
114
|
+
async dismiss(id) {
|
|
115
|
+
if (!(await root.open(sessionId)).some((record) => record.id === id)) throw new ApprovalRequestError("This approval is no longer here.", 404);
|
|
116
|
+
await root.dismiss(id);
|
|
117
|
+
},
|
|
118
|
+
dispose() {
|
|
119
|
+
lifetime.abort();
|
|
120
|
+
detach?.();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
//#endregion
|
|
125
|
+
export { APPROVAL_ROOT_SERVICE as n, ApprovalRequestError as r, createApprovalSession as t };
|
|
126
|
+
|
|
127
|
+
//# sourceMappingURL=approvalSession-Bs554RFc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approvalSession-Bs554RFc.mjs","names":[],"sources":["../src/services/approvalRoot/type.ts","../src/services/approvalSession/index.ts"],"sourcesContent":["import type { PoApprovalOperation, PoApprovalRequest } from '@agimon-ai/po-core/approval';\nimport type { PoHubSessionService } from '@agimon-ai/po-core/hubChannel';\nimport { defineServiceContract } from '@agimon-ai/po-core/serverPlugin';\n\nimport type { ApprovalRecord, ApprovalStore } from '../approvalStore/type';\n\n/** What a mounted session lends the root so approved calls run there and outcomes reach its agent. */\nexport interface ApprovalSessionBinding {\n /** True once an executor for this kind is registered; approved calls of other kinds wait. */\n ready(kind: PoApprovalOperation['kind']): boolean;\n run(record: ApprovalRecord, signal: AbortSignal): Promise<unknown>;\n /** Tells the agent the outcome. Resolves once the message is admitted. */\n deliver(record: ApprovalRecord, text: string): Promise<void>;\n /** Ends with the session, aborting a running call. */\n readonly signal: AbortSignal;\n}\n\nexport type ApprovalDecision = 'approve' | 'deny';\nexport type ApprovalOpenRecord = ApprovalRecord & {\n state: 'pending' | 'approved' | 'running' | 'interrupted';\n /** Derived from the host's session catalogue, never persisted or supplied by the client. */\n sessionGone?: true;\n /** The owning session's name and workspace folder, from the same catalogue, for Home. */\n sessionName?: string;\n workspaceName?: string;\n};\n\nexport class ApprovalRequestError extends Error {\n constructor(\n message: string,\n readonly status: 404 | 409 = 409,\n ) {\n super(message);\n this.name = 'ApprovalRequestError';\n }\n}\n\nexport interface ApprovalRootOptions {\n store: ApprovalStore;\n sessions?: Pick<PoHubSessionService, 'isLive' | 'dormant' | 'revive' | 'scopes'>;\n setPendingCount?(sessionId: string, count: number): void;\n onError: (error: unknown) => void;\n}\n\nexport interface ApprovalRoot {\n submit(request: PoApprovalRequest): Promise<{ readonly id: string }>;\n /** A mounted session attaches; its approved calls run and undelivered outcomes reach its agent. */\n attach(sessionId: string, binding: ApprovalSessionBinding): () => void;\n /** Runs what an attached session can run now, for example after an executor registers. */\n drain(sessionId: string): Promise<void>;\n /** The person's answer. Refuses a stale revision or an already decided request. */\n decide(id: string, revision: number, decision: ApprovalDecision): Promise<ApprovalRecord>;\n /** The agent takes back its own pending request. */\n withdraw(sessionId: string, id: string): Promise<ApprovalRecord | undefined>;\n /** The person clears a pending or approved call only when its session is gone. */\n dismiss(id: string, revision?: number): Promise<ApprovalRecord>;\n /** Pending, approved, running and not-yet-reported uncertain outcomes, oldest first, for Activity. */\n open(sessionId?: string): Promise<ApprovalOpenRecord[]>;\n onChanged(listener: (sessionId: string) => void): () => void;\n dispose(): void;\n}\n\nexport const APPROVAL_ROOT_SERVICE = defineServiceContract<ApprovalRoot>('po/approval-root', { scope: 'global' });\n","import type { PoApprovalExecutor, PoApprovalOperation, PoApprovalPrompt } from '@agimon-ai/po-core/approval';\n\nimport { ApprovalRequestError, type ApprovalSessionBinding } from '../approvalRoot/type';\nimport type { ApprovalRecord } from '../approvalStore/type';\nimport type { ApprovalSession, ApprovalSessionOptions } from './type';\n\nconst OUTCOME_MESSAGE_TYPE = 'approval-outcome';\n\nfunction prompt(record: ApprovalRecord): PoApprovalPrompt {\n const { title, summary, details } = record.description;\n return {\n id: record.id,\n toolCallId: record.toolCallId,\n operation: record.operation,\n requirement: record.requirement,\n title,\n summary,\n ...(details === undefined ? {} : { details }),\n };\n}\n\n/** One session's view of the global queue: it submits only its own calls and runs only its own approved ones. */\nexport function createApprovalSession(options: ApprovalSessionOptions): ApprovalSession {\n const { root, sessionId } = options;\n const executors = new Map<PoApprovalOperation['kind'], PoApprovalExecutor>();\n const lifetime = new AbortController();\n const binding: ApprovalSessionBinding = {\n signal: lifetime.signal,\n ready: (kind) => executors.has(kind),\n run: (record, signal) => {\n const executor = executors.get(record.operation.kind);\n if (executor === undefined) throw new Error(`Nothing in this session can run ${record.operation.name}.`);\n return executor(\n {\n approvalId: record.id,\n operation: record.operation,\n input: record.input,\n ...(record.argumentFormat === undefined ? {} : { argumentFormat: record.argumentFormat }),\n },\n signal,\n );\n },\n deliver: async (record, text) => {\n if (options.session?.admitMessage === undefined) throw new Error('This session cannot receive messages.');\n await options.session.admitMessage(\n { type: OUTCOME_MESSAGE_TYPE, text, details: { approvalId: record.id, state: record.state } },\n 'steer',\n `approval:${record.id}:${record.state}`,\n );\n },\n };\n let detach: (() => void) | undefined;\n const voiceTarget = async (): Promise<ApprovalRecord | undefined> => {\n const pending = (await root.open(sessionId)).filter((record) => record.state === 'pending');\n return pending.length === 1 && pending[0]!.requirement === 'ask' ? pending[0] : undefined;\n };\n return {\n start() {\n if (!lifetime.signal.aborted && detach === undefined) detach = root.attach(sessionId, binding);\n },\n service: {\n async submit(request) {\n if (request.sessionId !== sessionId) throw new Error('A session can only ask about its own calls.');\n return root.submit(request);\n },\n registerExecutor(kind, run) {\n executors.set(kind, run);\n void root.drain(sessionId);\n return () => {\n if (executors.get(kind) === run) executors.delete(kind);\n };\n },\n },\n async voiceCurrent() {\n const record = await voiceTarget();\n return record === undefined ? undefined : prompt(record);\n },\n async voiceAnswer(approvalId, decision) {\n const record = await voiceTarget();\n if (record?.id !== approvalId) return false;\n try {\n await root.decide(record.id, record.revision, decision === 'approved' ? 'approve' : 'deny');\n return true;\n } catch {\n // Answered elsewhere first, or changed meanwhile: this answer did nothing.\n return false;\n }\n },\n withdraw: (id) => root.withdraw(sessionId, id),\n async cards() {\n return (await root.open(sessionId)).map((record) => ({\n ...prompt(record),\n revision: record.revision,\n state: record.state,\n ...(record.sessionGone === true ? { sessionGone: true as const } : {}),\n // Home reads the same store, so live frames keep the names snapshots carry.\n ...(record.sessionName === undefined ? {} : { sessionName: record.sessionName.slice(0, 200) }),\n ...(record.workspaceName === undefined ? {} : { workspaceName: record.workspaceName.slice(0, 200) }),\n ...(record.reason === undefined ? {} : { reason: record.reason }),\n }));\n },\n onChanged(listener) {\n return root.onChanged((changed) => {\n if (changed === sessionId) listener();\n });\n },\n async decide(id, revision, decision) {\n // Only this session's own requests; an id from another session reads as gone.\n if (!(await root.open(sessionId)).some((record) => record.id === id))\n throw new ApprovalRequestError('This approval is no longer here.', 404);\n await root.decide(id, revision, decision);\n },\n async dismiss(id) {\n if (!(await root.open(sessionId)).some((record) => record.id === id))\n throw new ApprovalRequestError('This approval is no longer here.', 404);\n await root.dismiss(id);\n },\n dispose() {\n lifetime.abort();\n detach?.();\n },\n };\n}\n"],"mappings":";;AA2BA,IAAa,uBAAb,cAA0C,MAAM;CAGnC;CAFX,YACE,SACA,SAA6B,KAC7B;EACA,MAAM,OAAO;EAFJ,KAAA,SAAA;EAGT,KAAK,OAAO;CACd;AACF;AA2BA,MAAa,wBAAwB,sBAAoC,oBAAoB,EAAE,OAAO,SAAS,CAAC;;;ACxDhH,MAAM,uBAAuB;AAE7B,SAAS,OAAO,QAA0C;CACxD,MAAM,EAAE,OAAO,SAAS,YAAY,OAAO;CAC3C,OAAO;EACL,IAAI,OAAO;EACX,YAAY,OAAO;EACnB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