@x-otto/ask 0.0.0-stage → 0.3.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/dist/index.d.mts +45 -0
- package/dist/index.mjs +443 -0
- package/package.json +24 -4
- package/README.md +0 -3
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AnyAgentTool, AskClient, AskDeadline, AskDeadline as AskDeadline$1, AskOutcome, AskOutcome as AskOutcome$1, HitlAnswer, HitlAskRequest, PendingAsk } from "@x-otto/kernel";
|
|
2
|
+
|
|
3
|
+
//#region src/pending-store.d.ts
|
|
4
|
+
interface PendingAskStore {
|
|
5
|
+
save(sessionId: string, pending: readonly PendingAsk[]): Promise<void>;
|
|
6
|
+
load(sessionId: string): Promise<readonly PendingAsk[]>;
|
|
7
|
+
clear(sessionId: string): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
declare function createFilePendingAskStore(directory: string): PendingAskStore;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/grill-me.d.ts
|
|
12
|
+
declare const GRILL_ME_GUIDANCE: string;
|
|
13
|
+
declare function createGrillMeTool(hitlAsk: () => AskClient<HitlAskRequest, HitlAnswer> | undefined): AnyAgentTool;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/index.d.ts
|
|
16
|
+
interface AskPendingEntry<Req> {
|
|
17
|
+
requestId: string;
|
|
18
|
+
request: Req;
|
|
19
|
+
}
|
|
20
|
+
type AskBrokerCommitEvent<Req, Res> = {
|
|
21
|
+
phase: 'pending';
|
|
22
|
+
requestId: string;
|
|
23
|
+
request: Req;
|
|
24
|
+
} | {
|
|
25
|
+
phase: 'settled';
|
|
26
|
+
requestId: string;
|
|
27
|
+
request: Req;
|
|
28
|
+
outcome: AskOutcome$1<Res>;
|
|
29
|
+
};
|
|
30
|
+
interface AskBrokerEmit<Req, Res> {
|
|
31
|
+
commit?(event: AskBrokerCommitEvent<Req, Res>): void;
|
|
32
|
+
}
|
|
33
|
+
interface AskBrokerOpenOptions {
|
|
34
|
+
unavailable?: boolean;
|
|
35
|
+
}
|
|
36
|
+
interface AskBroker<Req, Res> {
|
|
37
|
+
open(request: Req, deadline: AskDeadline$1, options?: AskBrokerOpenOptions): Promise<AskOutcome$1<Res>>;
|
|
38
|
+
respond(requestId: string, value: Res | undefined): void;
|
|
39
|
+
pending(): readonly AskPendingEntry<Req>[];
|
|
40
|
+
pendingCount(): number;
|
|
41
|
+
dispose(): void;
|
|
42
|
+
}
|
|
43
|
+
declare function createAskBroker<Req, Res>(emit: AskBrokerEmit<Req, Res>, generateId: () => string): AskBroker<Req, Res>;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { AskBroker, AskBrokerCommitEvent, AskBrokerEmit, AskBrokerOpenOptions, type AskDeadline, type AskOutcome, AskPendingEntry, GRILL_ME_GUIDANCE, type PendingAskStore, createAskBroker, createFilePendingAskStore, createGrillMeTool };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { readFile, rename, unlink } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { writeFileAtomic } from "@x-otto/atomic-fs";
|
|
4
|
+
import { objectSchema } from "@x-otto/tool-schema";
|
|
5
|
+
import { NO_EFFECTS, TOOL_NAMES, defineTool, textToolContent } from "@x-otto/kernel";
|
|
6
|
+
//#region src/pending-store.ts
|
|
7
|
+
const SAFE_SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
8
|
+
function fileNameOf(sessionId) {
|
|
9
|
+
if (!SAFE_SESSION_ID.test(sessionId)) throw new Error("invalid session id for pending ask store");
|
|
10
|
+
return `${sessionId}.json`;
|
|
11
|
+
}
|
|
12
|
+
function isMissing(error) {
|
|
13
|
+
return error.code === "ENOENT";
|
|
14
|
+
}
|
|
15
|
+
function createFilePendingAskStore(directory) {
|
|
16
|
+
const pathOf = (sessionId) => join(directory, fileNameOf(sessionId));
|
|
17
|
+
const queues = /* @__PURE__ */ new Map();
|
|
18
|
+
const serialize = (sessionId, operation) => {
|
|
19
|
+
const next = (queues.get(sessionId) ?? Promise.resolve()).catch(() => void 0).then(operation);
|
|
20
|
+
const settled = next.catch(() => void 0);
|
|
21
|
+
queues.set(sessionId, settled);
|
|
22
|
+
settled.then(() => {
|
|
23
|
+
if (queues.get(sessionId) === settled) queues.delete(sessionId);
|
|
24
|
+
});
|
|
25
|
+
return next;
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
async save(sessionId, pending) {
|
|
29
|
+
const target = pathOf(sessionId);
|
|
30
|
+
const snapshot = JSON.stringify({
|
|
31
|
+
version: 1,
|
|
32
|
+
pending
|
|
33
|
+
});
|
|
34
|
+
return serialize(sessionId, async () => {
|
|
35
|
+
await writeFileAtomic(target, snapshot);
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
async load(sessionId) {
|
|
39
|
+
const target = pathOf(sessionId);
|
|
40
|
+
await (queues.get(sessionId) ?? Promise.resolve());
|
|
41
|
+
let text;
|
|
42
|
+
try {
|
|
43
|
+
text = await readFile(target, "utf8");
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (isMissing(error)) return [];
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
let raw;
|
|
49
|
+
try {
|
|
50
|
+
raw = JSON.parse(text);
|
|
51
|
+
} catch {
|
|
52
|
+
await rename(target, `${target}.corrupt`).catch(() => void 0);
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
return raw.version === 1 && Array.isArray(raw.pending) ? raw.pending : [];
|
|
56
|
+
},
|
|
57
|
+
async clear(sessionId) {
|
|
58
|
+
const target = pathOf(sessionId);
|
|
59
|
+
return serialize(sessionId, async () => {
|
|
60
|
+
try {
|
|
61
|
+
await unlink(target);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (!isMissing(error)) throw error;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/grill-me.ts
|
|
71
|
+
const AUTO_PREFIX = "(no human reachable; adopted the recommended answer)";
|
|
72
|
+
const NOT_ASKED_TEXT = "(not asked: an earlier question in this batch timed out or was cancelled, so asking stopped)";
|
|
73
|
+
const NO_HUMAN_NO_RECOMMENDATION = "(no human reachable; no recommendation was given — decide yourself)";
|
|
74
|
+
const NO_ANSWER = "The question was answered with no selection and no freeform text.";
|
|
75
|
+
const UNANSWERED_PREFIX = "(no human answer; the question was not answered)";
|
|
76
|
+
const MAX_QUESTIONS = 3;
|
|
77
|
+
const grillBaseSchema = objectSchema({
|
|
78
|
+
questions: {
|
|
79
|
+
type: "array",
|
|
80
|
+
optional: true,
|
|
81
|
+
max: MAX_QUESTIONS,
|
|
82
|
+
items: {
|
|
83
|
+
type: "object",
|
|
84
|
+
properties: {
|
|
85
|
+
header: {
|
|
86
|
+
type: "string",
|
|
87
|
+
max: 24,
|
|
88
|
+
optional: true
|
|
89
|
+
},
|
|
90
|
+
question: { type: "string" },
|
|
91
|
+
recommendation: { type: "string" },
|
|
92
|
+
options: {
|
|
93
|
+
type: "array",
|
|
94
|
+
optional: true,
|
|
95
|
+
items: {
|
|
96
|
+
type: "object",
|
|
97
|
+
properties: {
|
|
98
|
+
label: { type: "string" },
|
|
99
|
+
rationale: {
|
|
100
|
+
type: "string",
|
|
101
|
+
optional: true
|
|
102
|
+
},
|
|
103
|
+
recommended: {
|
|
104
|
+
type: "boolean",
|
|
105
|
+
optional: true
|
|
106
|
+
},
|
|
107
|
+
preview: {
|
|
108
|
+
type: "string",
|
|
109
|
+
optional: true
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
allowFreeform: {
|
|
115
|
+
type: "boolean",
|
|
116
|
+
optional: true
|
|
117
|
+
},
|
|
118
|
+
multiSelect: {
|
|
119
|
+
type: "boolean",
|
|
120
|
+
optional: true
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
header: {
|
|
126
|
+
type: "string",
|
|
127
|
+
max: 24,
|
|
128
|
+
optional: true
|
|
129
|
+
},
|
|
130
|
+
question: {
|
|
131
|
+
type: "string",
|
|
132
|
+
optional: true
|
|
133
|
+
},
|
|
134
|
+
background: { type: "string" },
|
|
135
|
+
recommendation: {
|
|
136
|
+
type: "string",
|
|
137
|
+
optional: true
|
|
138
|
+
},
|
|
139
|
+
options: {
|
|
140
|
+
type: "array",
|
|
141
|
+
optional: true,
|
|
142
|
+
items: {
|
|
143
|
+
type: "object",
|
|
144
|
+
properties: {
|
|
145
|
+
label: { type: "string" },
|
|
146
|
+
rationale: {
|
|
147
|
+
type: "string",
|
|
148
|
+
optional: true
|
|
149
|
+
},
|
|
150
|
+
recommended: {
|
|
151
|
+
type: "boolean",
|
|
152
|
+
optional: true
|
|
153
|
+
},
|
|
154
|
+
preview: {
|
|
155
|
+
type: "string",
|
|
156
|
+
optional: true
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
allowFreeform: {
|
|
162
|
+
type: "boolean",
|
|
163
|
+
optional: true
|
|
164
|
+
},
|
|
165
|
+
multiSelect: {
|
|
166
|
+
type: "boolean",
|
|
167
|
+
optional: true
|
|
168
|
+
},
|
|
169
|
+
evidence: {
|
|
170
|
+
type: "array",
|
|
171
|
+
optional: true,
|
|
172
|
+
items: { type: "string" }
|
|
173
|
+
}
|
|
174
|
+
}, { rejectUnknownFields: true });
|
|
175
|
+
function withoutBlankOptions(item) {
|
|
176
|
+
if (item.options === void 0) return item;
|
|
177
|
+
const options = item.options.filter((option) => option.label.trim() !== "");
|
|
178
|
+
if (options.length === item.options.length) return item;
|
|
179
|
+
return {
|
|
180
|
+
...item.header !== void 0 ? { header: item.header } : {},
|
|
181
|
+
question: item.question,
|
|
182
|
+
...item.recommendation !== void 0 ? { recommendation: item.recommendation } : {},
|
|
183
|
+
...options.length === 0 ? {} : { options },
|
|
184
|
+
...item.allowFreeform !== void 0 ? { allowFreeform: item.allowFreeform } : {},
|
|
185
|
+
...item.multiSelect !== void 0 ? { multiSelect: item.multiSelect } : {}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function questionsOf(input) {
|
|
189
|
+
if (input.questions !== void 0 && input.questions.length > 0) return input.questions.map((item) => withoutBlankOptions(item));
|
|
190
|
+
if (input.question !== void 0 && input.question.length > 0) return [withoutBlankOptions({
|
|
191
|
+
...input.header !== void 0 ? { header: input.header } : {},
|
|
192
|
+
question: input.question,
|
|
193
|
+
...input.recommendation !== void 0 ? { recommendation: input.recommendation } : {},
|
|
194
|
+
...input.options !== void 0 ? { options: input.options } : {},
|
|
195
|
+
...input.allowFreeform !== void 0 ? { allowFreeform: input.allowFreeform } : {},
|
|
196
|
+
...input.multiSelect !== void 0 ? { multiSelect: input.multiSelect } : {}
|
|
197
|
+
})];
|
|
198
|
+
return [];
|
|
199
|
+
}
|
|
200
|
+
function answerText(outcome) {
|
|
201
|
+
const answer = outcome.value;
|
|
202
|
+
const selected = answer.selectedOptionLabels.join(", ");
|
|
203
|
+
if (answer.freeform.length > 0) return selected.length > 0 ? `${selected}\n${answer.freeform}` : answer.freeform;
|
|
204
|
+
return selected.length > 0 ? selected : NO_ANSWER;
|
|
205
|
+
}
|
|
206
|
+
function unansweredText(status, recommendation) {
|
|
207
|
+
const head = status === "cancelled" ? "The question was cancelled; no human answer was received." : "The question timed out; no human answer was received.";
|
|
208
|
+
return recommendation === void 0 ? head : `${head}\n${UNANSWERED_PREFIX}\nSuggested answer (not adopted — decide yourself): ${recommendation}`;
|
|
209
|
+
}
|
|
210
|
+
const GRILL_ME_GUIDANCE = `When the user must decide — a value you cannot infer, a choice between reasonable options, or approval before a hard-to-reverse action — call ${TOOL_NAMES.grillMe} instead of listing questions in your reply. ${TOOL_NAMES.grillMe} is advisory and never authorizes side effects; ask one clear question at a time.`;
|
|
211
|
+
function createGrillMeTool(hitlAsk) {
|
|
212
|
+
return defineTool({
|
|
213
|
+
name: TOOL_NAMES.grillMe,
|
|
214
|
+
description: "Ask the human a clarifying question and return their answer, or the recommendation when no human is available. background is required (context for the human). When using questions=[…], pass at most 3 entries, each with a question and a recommendation (options need a label); the single-question form is allowed without a recommendation, and no human means you decide.",
|
|
215
|
+
schema: grillBaseSchema,
|
|
216
|
+
effects: NO_EFFECTS,
|
|
217
|
+
async execute(input, ctx) {
|
|
218
|
+
const { background, evidence } = input;
|
|
219
|
+
const list = questionsOf(input);
|
|
220
|
+
if (list.length === 0) return {
|
|
221
|
+
content: textToolContent("grill_me needs one question: pass question=… or questions=[…] (1–3)."),
|
|
222
|
+
isError: true,
|
|
223
|
+
errorKind: "validation"
|
|
224
|
+
};
|
|
225
|
+
if (list.length > MAX_QUESTIONS) return {
|
|
226
|
+
content: textToolContent(`grill_me asks at most ${MAX_QUESTIONS} questions at once; got ${list.length}.`),
|
|
227
|
+
isError: true,
|
|
228
|
+
errorKind: "validation"
|
|
229
|
+
};
|
|
230
|
+
if (list.some((item) => item.question.trim() === "")) return {
|
|
231
|
+
content: textToolContent("grill_me needs a non-empty question for every entry."),
|
|
232
|
+
isError: true,
|
|
233
|
+
errorKind: "validation"
|
|
234
|
+
};
|
|
235
|
+
const ask = hitlAsk();
|
|
236
|
+
const blocks = [];
|
|
237
|
+
let adopted = 0;
|
|
238
|
+
let answered = 0;
|
|
239
|
+
let unanswered = 0;
|
|
240
|
+
const results = [];
|
|
241
|
+
const multi = list.length > 1;
|
|
242
|
+
let resolvedBy = ask === void 0 ? "unavailable" : void 0;
|
|
243
|
+
let answerDetails;
|
|
244
|
+
let interrupted = false;
|
|
245
|
+
for (const [index, item] of list.entries()) {
|
|
246
|
+
const label = multi ? `Q${index + 1}: ${item.question}\n` : "";
|
|
247
|
+
if (interrupted) {
|
|
248
|
+
blocks.push(`${label}${NOT_ASKED_TEXT}`);
|
|
249
|
+
results.push({
|
|
250
|
+
question: item.question,
|
|
251
|
+
outcome: "not_asked"
|
|
252
|
+
});
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (ask === void 0) {
|
|
256
|
+
if (item.recommendation === void 0) {
|
|
257
|
+
unanswered += 1;
|
|
258
|
+
blocks.push(`${label}${NO_HUMAN_NO_RECOMMENDATION}`);
|
|
259
|
+
results.push({
|
|
260
|
+
question: item.question,
|
|
261
|
+
outcome: "unanswered"
|
|
262
|
+
});
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
adopted += 1;
|
|
266
|
+
blocks.push(`${label}${AUTO_PREFIX}\n${item.recommendation}`);
|
|
267
|
+
results.push({
|
|
268
|
+
question: item.question,
|
|
269
|
+
outcome: "adopted",
|
|
270
|
+
recommendation: item.recommendation
|
|
271
|
+
});
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
const request = {
|
|
275
|
+
sessionId: ctx.sessionId,
|
|
276
|
+
question: item.question,
|
|
277
|
+
...item.header !== void 0 ? { header: item.header } : {},
|
|
278
|
+
...item.recommendation !== void 0 ? { recommendation: item.recommendation } : {},
|
|
279
|
+
...background !== void 0 ? { background } : {},
|
|
280
|
+
...item.options !== void 0 ? { options: item.options } : {},
|
|
281
|
+
allowFreeform: item.allowFreeform ?? true,
|
|
282
|
+
multiSelect: item.multiSelect ?? false,
|
|
283
|
+
...evidence !== void 0 ? { evidence } : {}
|
|
284
|
+
};
|
|
285
|
+
const outcome = await ask.ask(request, { signal: ctx.signal });
|
|
286
|
+
resolvedBy = outcome.status;
|
|
287
|
+
if (outcome.status === "answered") {
|
|
288
|
+
answered += 1;
|
|
289
|
+
answerDetails = {
|
|
290
|
+
selectedOptionLabels: outcome.value.selectedOptionLabels,
|
|
291
|
+
freeform: outcome.value.freeform
|
|
292
|
+
};
|
|
293
|
+
blocks.push(`${label}${answerText(outcome)}`);
|
|
294
|
+
results.push({
|
|
295
|
+
question: item.question,
|
|
296
|
+
outcome: "answered",
|
|
297
|
+
selectedOptionLabels: outcome.value.selectedOptionLabels,
|
|
298
|
+
freeform: outcome.value.freeform
|
|
299
|
+
});
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (outcome.status === "unavailable") {
|
|
303
|
+
if (item.recommendation === void 0) {
|
|
304
|
+
unanswered += 1;
|
|
305
|
+
blocks.push(`${label}${NO_HUMAN_NO_RECOMMENDATION}`);
|
|
306
|
+
results.push({
|
|
307
|
+
question: item.question,
|
|
308
|
+
outcome: "unanswered"
|
|
309
|
+
});
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
adopted += 1;
|
|
313
|
+
blocks.push(`${label}${AUTO_PREFIX}\n${item.recommendation}`);
|
|
314
|
+
results.push({
|
|
315
|
+
question: item.question,
|
|
316
|
+
outcome: "adopted",
|
|
317
|
+
recommendation: item.recommendation
|
|
318
|
+
});
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
unanswered += 1;
|
|
322
|
+
interrupted = true;
|
|
323
|
+
blocks.push(`${label}${unansweredText(outcome.status, item.recommendation)}`);
|
|
324
|
+
results.push({
|
|
325
|
+
question: item.question,
|
|
326
|
+
outcome: outcome.status,
|
|
327
|
+
...item.recommendation !== void 0 ? { recommendation: item.recommendation } : {}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
return {
|
|
331
|
+
content: textToolContent(blocks.join("\n\n")),
|
|
332
|
+
details: {
|
|
333
|
+
asked: list.length,
|
|
334
|
+
answered,
|
|
335
|
+
adopted,
|
|
336
|
+
unanswered,
|
|
337
|
+
results,
|
|
338
|
+
...list.length === 1 && resolvedBy !== void 0 ? { resolvedBy } : {},
|
|
339
|
+
...list.length === 1 ? { question: list[0].question } : {},
|
|
340
|
+
...list.length === 1 && answerDetails !== void 0 ? answerDetails : {}
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region src/index.ts
|
|
348
|
+
function createAskBroker(emit, generateId) {
|
|
349
|
+
const pending = /* @__PURE__ */ new Map();
|
|
350
|
+
let disposed = false;
|
|
351
|
+
function commit(event) {
|
|
352
|
+
try {
|
|
353
|
+
emit.commit?.(event);
|
|
354
|
+
} catch {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
function release(entry, outcome) {
|
|
359
|
+
if (entry.timer !== void 0) clearTimeout(entry.timer);
|
|
360
|
+
if (entry.onAbort !== void 0) entry.signal.removeEventListener("abort", entry.onAbort);
|
|
361
|
+
pending.delete(entry.requestId);
|
|
362
|
+
commit({
|
|
363
|
+
phase: "settled",
|
|
364
|
+
requestId: entry.requestId,
|
|
365
|
+
request: entry.request,
|
|
366
|
+
outcome
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
function settle(entry, outcome) {
|
|
370
|
+
if (entry.settled) return;
|
|
371
|
+
entry.settled = true;
|
|
372
|
+
release(entry, outcome);
|
|
373
|
+
entry.settle(outcome);
|
|
374
|
+
}
|
|
375
|
+
return {
|
|
376
|
+
open(request, deadline, options = {}) {
|
|
377
|
+
const requestId = generateId();
|
|
378
|
+
return new Promise((resolve) => {
|
|
379
|
+
const signal = deadline.signal;
|
|
380
|
+
const entry = {
|
|
381
|
+
requestId,
|
|
382
|
+
request,
|
|
383
|
+
settle: resolve,
|
|
384
|
+
timer: void 0,
|
|
385
|
+
signal,
|
|
386
|
+
onAbort: void 0,
|
|
387
|
+
settled: false
|
|
388
|
+
};
|
|
389
|
+
if (signal.aborted) {
|
|
390
|
+
settle(entry, { status: "cancelled" });
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (disposed) {
|
|
394
|
+
settle(entry, { status: "cancelled" });
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (options.unavailable === true) {
|
|
398
|
+
settle(entry, { status: "unavailable" });
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const onAbort = () => settle(entry, { status: "cancelled" });
|
|
402
|
+
entry.onAbort = onAbort;
|
|
403
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
404
|
+
if (deadline.timeoutMs !== void 0) entry.timer = setTimeout(() => settle(entry, { status: "timed-out" }), deadline.timeoutMs);
|
|
405
|
+
pending.set(requestId, entry);
|
|
406
|
+
commit({
|
|
407
|
+
phase: "pending",
|
|
408
|
+
requestId,
|
|
409
|
+
request
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
},
|
|
413
|
+
respond(requestId, value) {
|
|
414
|
+
const entry = pending.get(requestId);
|
|
415
|
+
if (entry === void 0) return;
|
|
416
|
+
if (value === void 0) {
|
|
417
|
+
settle(entry, { status: "cancelled" });
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
settle(entry, {
|
|
421
|
+
status: "answered",
|
|
422
|
+
value,
|
|
423
|
+
by: "human"
|
|
424
|
+
});
|
|
425
|
+
},
|
|
426
|
+
pending() {
|
|
427
|
+
return [...pending.values()].map((entry) => ({
|
|
428
|
+
requestId: entry.requestId,
|
|
429
|
+
request: entry.request
|
|
430
|
+
}));
|
|
431
|
+
},
|
|
432
|
+
pendingCount() {
|
|
433
|
+
return pending.size;
|
|
434
|
+
},
|
|
435
|
+
dispose() {
|
|
436
|
+
if (disposed) return;
|
|
437
|
+
disposed = true;
|
|
438
|
+
for (const entry of [...pending.values()]) settle(entry, { status: "cancelled" });
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
//#endregion
|
|
443
|
+
export { GRILL_ME_GUIDANCE, createAskBroker, createFilePendingAskStore, createGrillMeTool };
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-otto/ask",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0-beta.0",
|
|
4
|
+
"description": "请求-应答基元:可取消、带 deadline、只终结一次的 ask broker",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"import": "./dist/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@x-otto/atomic-fs": "^0.3.0-beta.0",
|
|
19
|
+
"@x-otto/kernel": "^0.3.0-beta.0",
|
|
20
|
+
"@x-otto/tool-schema": "^0.3.0-beta.0"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"tag": "beta"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/README.md
DELETED