@deepagents/experimental 6.1.0 → 6.1.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.
- package/dist/index.js +441 -19
- package/dist/index.js.map +4 -4
- package/dist/zukhruf/control-plane/agent-declaration-registry.d.ts +1 -0
- package/dist/zukhruf/control-plane/agent-declaration-registry.d.ts.map +1 -1
- package/dist/zukhruf/index.d.ts +1 -0
- package/dist/zukhruf/index.d.ts.map +1 -1
- package/dist/zukhruf/index.js +441 -19
- package/dist/zukhruf/index.js.map +4 -4
- package/dist/zukhruf/protocol/parse.d.ts +3 -0
- package/dist/zukhruf/protocol/parse.d.ts.map +1 -0
- package/dist/zukhruf/protocol/session.d.ts +23 -0
- package/dist/zukhruf/protocol/session.d.ts.map +1 -0
- package/dist/zukhruf/protocol/validator.d.ts +64 -0
- package/dist/zukhruf/protocol/validator.d.ts.map +1 -0
- package/dist/zukhruf/runtime/agent-runtime.d.ts +15 -0
- package/dist/zukhruf/runtime/agent-runtime.d.ts.map +1 -1
- package/package.json +4 -2
|
@@ -5,5 +5,6 @@ export declare class AgentDeclarationRegistry {
|
|
|
5
5
|
readonly root: AgentDeclaration;
|
|
6
6
|
constructor(root: AgentDeclaration);
|
|
7
7
|
get(name: string): AgentDeclaration | undefined;
|
|
8
|
+
values(): IterableIterator<AgentDeclaration>;
|
|
8
9
|
}
|
|
9
10
|
//# sourceMappingURL=agent-declaration-registry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-declaration-registry.d.ts","sourceRoot":"","sources":["../../../src/zukhruf/control-plane/agent-declaration-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,wEAAwE;AACxE,qBAAa,wBAAwB;;IACnC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;gBAIpB,IAAI,EAAE,gBAAgB;IAKlC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;
|
|
1
|
+
{"version":3,"file":"agent-declaration-registry.d.ts","sourceRoot":"","sources":["../../../src/zukhruf/control-plane/agent-declaration-registry.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,wEAAwE;AACxE,qBAAa,wBAAwB;;IACnC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;gBAIpB,IAAI,EAAE,gBAAgB;IAKlC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAI/C,MAAM,IAAI,gBAAgB,CAAC,gBAAgB,CAAC;CAgC7C"}
|
package/dist/zukhruf/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * from './instructions.ts';
|
|
|
8
8
|
export * from './mailbox/sqlite.store.ts';
|
|
9
9
|
export * from './mailbox/store.ts';
|
|
10
10
|
export * from './mailbox/types.ts';
|
|
11
|
+
export * from './protocol/session.ts';
|
|
11
12
|
export * from './queue/pg-boss.turn-queue.ts';
|
|
12
13
|
export * from './queue/turn-queue.ts';
|
|
13
14
|
export * from './render-turn.ts';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/zukhruf/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,YAAY,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AACxE,cAAc,4BAA4B,CAAC;AAC3C,YAAY,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACzE,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/zukhruf/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,YAAY,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AACxE,cAAc,4BAA4B,CAAC;AAC3C,YAAY,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACzE,cAAc,mBAAmB,CAAC;AAClC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC"}
|
package/dist/zukhruf/index.js
CHANGED
|
@@ -95,11 +95,11 @@ var AgentThread = class _AgentThread {
|
|
|
95
95
|
}
|
|
96
96
|
static fromMetadata(conversation, metadata) {
|
|
97
97
|
const treeId = metadata?.zukhrufTreeId;
|
|
98
|
-
const
|
|
99
|
-
if (typeof treeId !== "string" || !_AgentThread.#isRecord(
|
|
98
|
+
const zukhruf2 = metadata?.zukhruf;
|
|
99
|
+
if (typeof treeId !== "string" || !_AgentThread.#isRecord(zukhruf2)) {
|
|
100
100
|
return void 0;
|
|
101
101
|
}
|
|
102
|
-
const { path: path2, parentChatId, declarationName, lastTurnId } =
|
|
102
|
+
const { path: path2, parentChatId, declarationName, lastTurnId } = zukhruf2;
|
|
103
103
|
if (typeof path2 !== "string" || parentChatId !== null && typeof parentChatId !== "string" || typeof declarationName !== "string" || lastTurnId !== void 0 && typeof lastTurnId !== "string") {
|
|
104
104
|
return void 0;
|
|
105
105
|
}
|
|
@@ -234,6 +234,9 @@ var AgentDeclarationRegistry = class {
|
|
|
234
234
|
get(name) {
|
|
235
235
|
return this.#declarations.get(name);
|
|
236
236
|
}
|
|
237
|
+
values() {
|
|
238
|
+
return this.#declarations.values();
|
|
239
|
+
}
|
|
237
240
|
#register(declaration, visited) {
|
|
238
241
|
if (visited.has(declaration)) return;
|
|
239
242
|
visited.add(declaration);
|
|
@@ -518,14 +521,14 @@ var AgentHistoryForker = class {
|
|
|
518
521
|
let claimed;
|
|
519
522
|
await this.#store.updateChat(child.conversation.chatId, (chat2) => {
|
|
520
523
|
const metadata = chat2.metadata;
|
|
521
|
-
const
|
|
522
|
-
if (!isRecord(
|
|
524
|
+
const zukhruf2 = metadata?.zukhruf;
|
|
525
|
+
if (!isRecord(zukhruf2)) {
|
|
523
526
|
throw new Error(
|
|
524
527
|
`spawn_agent: child chat "${child.conversation.chatId}" is missing Zukhruf metadata`
|
|
525
528
|
);
|
|
526
529
|
}
|
|
527
|
-
if (
|
|
528
|
-
claimed = parseHistoryForkRecord(
|
|
530
|
+
if (zukhruf2.historyFork !== void 0) {
|
|
531
|
+
claimed = parseHistoryForkRecord(zukhruf2.historyFork);
|
|
529
532
|
if (claimed.parentChatId !== proposed.parentChatId || claimed.forkTurns !== proposed.forkTurns) {
|
|
530
533
|
throw new Error(
|
|
531
534
|
`spawn_agent: agent path "${child.path}" already exists with a different fork_turns setting`
|
|
@@ -537,7 +540,7 @@ var AgentHistoryForker = class {
|
|
|
537
540
|
return {
|
|
538
541
|
metadata: {
|
|
539
542
|
...metadata,
|
|
540
|
-
zukhruf: { ...
|
|
543
|
+
zukhruf: { ...zukhruf2, historyFork: proposed }
|
|
541
544
|
}
|
|
542
545
|
};
|
|
543
546
|
});
|
|
@@ -624,13 +627,13 @@ var AgentHistoryForker = class {
|
|
|
624
627
|
let sourceMessageIds;
|
|
625
628
|
await this.#store.updateChat(child.conversation.chatId, (chat2) => {
|
|
626
629
|
const metadata = chat2.metadata;
|
|
627
|
-
const
|
|
628
|
-
if (!isRecord(
|
|
630
|
+
const zukhruf2 = metadata?.zukhruf;
|
|
631
|
+
if (!isRecord(zukhruf2)) {
|
|
629
632
|
throw new Error(
|
|
630
633
|
`spawn_agent: child chat "${child.conversation.chatId}" is missing Zukhruf metadata`
|
|
631
634
|
);
|
|
632
635
|
}
|
|
633
|
-
const record = parseHistoryForkRecord(
|
|
636
|
+
const record = parseHistoryForkRecord(zukhruf2.historyFork);
|
|
634
637
|
if (record.sourceMessageIds !== void 0) {
|
|
635
638
|
sourceMessageIds = record.sourceMessageIds;
|
|
636
639
|
return void 0;
|
|
@@ -640,7 +643,7 @@ var AgentHistoryForker = class {
|
|
|
640
643
|
metadata: {
|
|
641
644
|
...metadata,
|
|
642
645
|
zukhruf: {
|
|
643
|
-
...
|
|
646
|
+
...zukhruf2,
|
|
644
647
|
historyFork: { ...record, sourceMessageIds: proposedIds }
|
|
645
648
|
}
|
|
646
649
|
}
|
|
@@ -1784,11 +1787,11 @@ var AgentTurnExecutor = class {
|
|
|
1784
1787
|
available = persisted;
|
|
1785
1788
|
return void 0;
|
|
1786
1789
|
}
|
|
1787
|
-
const
|
|
1790
|
+
const zukhruf2 = isRecord2(metadata?.zukhruf) ? metadata.zukhruf : {};
|
|
1788
1791
|
return {
|
|
1789
1792
|
metadata: {
|
|
1790
1793
|
...metadata,
|
|
1791
|
-
zukhruf: { ...
|
|
1794
|
+
zukhruf: { ...zukhruf2, skills: available }
|
|
1792
1795
|
}
|
|
1793
1796
|
};
|
|
1794
1797
|
});
|
|
@@ -1823,14 +1826,14 @@ ${communication.content}`;
|
|
|
1823
1826
|
}
|
|
1824
1827
|
};
|
|
1825
1828
|
function readSkills(metadata) {
|
|
1826
|
-
const
|
|
1827
|
-
if (!isRecord2(
|
|
1829
|
+
const zukhruf2 = metadata?.zukhruf;
|
|
1830
|
+
if (!isRecord2(zukhruf2) || !Object.hasOwn(zukhruf2, "skills")) {
|
|
1828
1831
|
return void 0;
|
|
1829
1832
|
}
|
|
1830
|
-
if (!Array.isArray(
|
|
1833
|
+
if (!Array.isArray(zukhruf2.skills) || !zukhruf2.skills.every(isSkill)) {
|
|
1831
1834
|
throw new Error("AgentRuntime: stored skill catalog is invalid");
|
|
1832
1835
|
}
|
|
1833
|
-
return
|
|
1836
|
+
return zukhruf2.skills;
|
|
1834
1837
|
}
|
|
1835
1838
|
function isSkill(value) {
|
|
1836
1839
|
return isRecord2(value) && typeof value.name === "string" && typeof value.description === "string" && typeof value.path === "string";
|
|
@@ -2119,11 +2122,21 @@ var AgentObservation = class {
|
|
|
2119
2122
|
}
|
|
2120
2123
|
}
|
|
2121
2124
|
async #headStreamId() {
|
|
2125
|
+
const scheduled = await this.#queue.getCurrentTurn(this.#conversation);
|
|
2126
|
+
if (scheduled) return scheduled.streamId;
|
|
2127
|
+
const chat2 = await this.#store.getChat(this.#conversation.chatId);
|
|
2128
|
+
if (!chat2) return void 0;
|
|
2129
|
+
if (chat2.userId !== this.#conversation.userId) {
|
|
2130
|
+
throw new Error(
|
|
2131
|
+
`chat "${this.#conversation.chatId}" belongs to user "${chat2.userId}", not "${this.#conversation.userId}"`
|
|
2132
|
+
);
|
|
2133
|
+
}
|
|
2122
2134
|
const head = await this.engine.headMessage();
|
|
2123
2135
|
return head?.name === "assistant" ? head.id : void 0;
|
|
2124
2136
|
}
|
|
2125
2137
|
};
|
|
2126
2138
|
var AgentRuntime = class {
|
|
2139
|
+
info;
|
|
2127
2140
|
#store;
|
|
2128
2141
|
#queue;
|
|
2129
2142
|
#streams;
|
|
@@ -2172,6 +2185,18 @@ var AgentRuntime = class {
|
|
|
2172
2185
|
this.#directory = directory;
|
|
2173
2186
|
this.#controlPlane = controlPlane;
|
|
2174
2187
|
this.#approvals = approvals;
|
|
2188
|
+
this.info = {
|
|
2189
|
+
root: declarations.root.name,
|
|
2190
|
+
agents: Array.from(declarations.values(), (declaration) => ({
|
|
2191
|
+
name: declaration.name,
|
|
2192
|
+
model: {
|
|
2193
|
+
provider: declaration.model.provider,
|
|
2194
|
+
modelId: declaration.model.modelId
|
|
2195
|
+
},
|
|
2196
|
+
tools: Object.keys(declaration.tools ?? {}).sort(),
|
|
2197
|
+
subagents: (declaration.subagents ?? []).map(({ name }) => name)
|
|
2198
|
+
}))
|
|
2199
|
+
};
|
|
2175
2200
|
this.#executor = new AgentTurnExecutor({
|
|
2176
2201
|
store: options.store,
|
|
2177
2202
|
streams,
|
|
@@ -2181,6 +2206,14 @@ var AgentRuntime = class {
|
|
|
2181
2206
|
multiAgentV2
|
|
2182
2207
|
});
|
|
2183
2208
|
}
|
|
2209
|
+
async createSession(conversation) {
|
|
2210
|
+
await this.#controlPlane.resolve(conversation);
|
|
2211
|
+
}
|
|
2212
|
+
async sessionExists(conversation) {
|
|
2213
|
+
const chat2 = await this.#store.getChat(conversation.chatId);
|
|
2214
|
+
if (!chat2 || chat2.userId !== conversation.userId) return false;
|
|
2215
|
+
return (await this.#directory.load(conversation))?.path.isRoot ?? false;
|
|
2216
|
+
}
|
|
2184
2217
|
async enqueue(conversation, turn) {
|
|
2185
2218
|
const streamId = await this.#controlPlane.enqueue(conversation, turn);
|
|
2186
2219
|
return { id: streamId, stream: this.#streams.watch(streamId) };
|
|
@@ -2472,6 +2505,386 @@ var SqliteMailboxStore = class extends MailboxStore {
|
|
|
2472
2505
|
}
|
|
2473
2506
|
};
|
|
2474
2507
|
|
|
2508
|
+
// packages/experimental/src/zukhruf/protocol/session.ts
|
|
2509
|
+
import { createUIMessageStreamResponse } from "ai";
|
|
2510
|
+
import { Hono } from "hono";
|
|
2511
|
+
import { bodyLimit } from "hono/body-limit";
|
|
2512
|
+
import { HTTPException as HTTPException3 } from "hono/http-exception";
|
|
2513
|
+
import { v5 as uuidv56, validate as validateUuid } from "uuid";
|
|
2514
|
+
import z9 from "zod";
|
|
2515
|
+
|
|
2516
|
+
// packages/experimental/src/zukhruf/protocol/validator.ts
|
|
2517
|
+
import { parse as parseContentType } from "fast-content-type-parse";
|
|
2518
|
+
import { createMiddleware } from "hono/factory";
|
|
2519
|
+
import { HTTPException as HTTPException2 } from "hono/http-exception";
|
|
2520
|
+
import z8 from "zod";
|
|
2521
|
+
|
|
2522
|
+
// packages/experimental/src/zukhruf/protocol/parse.ts
|
|
2523
|
+
import { HTTPException } from "hono/http-exception";
|
|
2524
|
+
import "zod";
|
|
2525
|
+
async function parse(schema, input) {
|
|
2526
|
+
const result = await schema.safeParseAsync(input);
|
|
2527
|
+
if (!result.success) {
|
|
2528
|
+
const errors = result.error.flatten((issue) => ({
|
|
2529
|
+
message: issue.message,
|
|
2530
|
+
code: issue.code,
|
|
2531
|
+
path: issue.path.join(".")
|
|
2532
|
+
})).fieldErrors;
|
|
2533
|
+
throw new HTTPException(400, {
|
|
2534
|
+
message: "Validation failed",
|
|
2535
|
+
cause: {
|
|
2536
|
+
code: "api/validation-failed",
|
|
2537
|
+
detail: "The input data is invalid",
|
|
2538
|
+
errors
|
|
2539
|
+
}
|
|
2540
|
+
});
|
|
2541
|
+
}
|
|
2542
|
+
return result.data;
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
// packages/experimental/src/zukhruf/protocol/validator.ts
|
|
2546
|
+
function validate(expectedContentTypeOrSelector, selector) {
|
|
2547
|
+
const expectedContentType = typeof expectedContentTypeOrSelector === "string" ? expectedContentTypeOrSelector : void 0;
|
|
2548
|
+
const _selector = typeof expectedContentTypeOrSelector === "function" ? expectedContentTypeOrSelector : selector;
|
|
2549
|
+
if (!_selector) {
|
|
2550
|
+
throw new Error("Selector function is required");
|
|
2551
|
+
}
|
|
2552
|
+
return createMiddleware(async (c, next) => {
|
|
2553
|
+
const ct = c.req.header("content-type");
|
|
2554
|
+
if (c.req.method === "GET" && ct) {
|
|
2555
|
+
throw new HTTPException2(415, {
|
|
2556
|
+
message: "Unsupported Media Type",
|
|
2557
|
+
cause: {
|
|
2558
|
+
code: "api/unsupported-media-type",
|
|
2559
|
+
detail: `GET requests cannot have a content type header`
|
|
2560
|
+
}
|
|
2561
|
+
});
|
|
2562
|
+
}
|
|
2563
|
+
if (expectedContentType) {
|
|
2564
|
+
void verifyContentType(ct, expectedContentType);
|
|
2565
|
+
}
|
|
2566
|
+
const contentType = ct ? parseContentType(ct) : null;
|
|
2567
|
+
let body = null;
|
|
2568
|
+
switch (contentType?.type) {
|
|
2569
|
+
case "application/json":
|
|
2570
|
+
body = await parseJson(c);
|
|
2571
|
+
break;
|
|
2572
|
+
case "application/x-www-form-urlencoded":
|
|
2573
|
+
case "multipart/form-data":
|
|
2574
|
+
body = await c.req.parseBody();
|
|
2575
|
+
break;
|
|
2576
|
+
default:
|
|
2577
|
+
body = {};
|
|
2578
|
+
}
|
|
2579
|
+
const payload = {
|
|
2580
|
+
body,
|
|
2581
|
+
query: parseQueryParams(c.req.query()),
|
|
2582
|
+
queries: parseQueriesParams(c.req.queries()),
|
|
2583
|
+
params: c.req.param(),
|
|
2584
|
+
headers: Object.fromEntries(
|
|
2585
|
+
Object.entries(c.req.header()).map(([k, v]) => [k, v ?? ""])
|
|
2586
|
+
)
|
|
2587
|
+
};
|
|
2588
|
+
const config = _selector(payload);
|
|
2589
|
+
const schema = z8.object(
|
|
2590
|
+
Object.entries(config).reduce(
|
|
2591
|
+
(acc, [key, value]) => {
|
|
2592
|
+
acc[key] = value.against;
|
|
2593
|
+
return acc;
|
|
2594
|
+
},
|
|
2595
|
+
{}
|
|
2596
|
+
)
|
|
2597
|
+
);
|
|
2598
|
+
const input = Object.entries(config).reduce(
|
|
2599
|
+
(acc, [key, value]) => {
|
|
2600
|
+
acc[key] = value.select;
|
|
2601
|
+
return acc;
|
|
2602
|
+
},
|
|
2603
|
+
{}
|
|
2604
|
+
);
|
|
2605
|
+
const parsed = await parse(schema, input);
|
|
2606
|
+
c.set("input", parsed);
|
|
2607
|
+
await next();
|
|
2608
|
+
});
|
|
2609
|
+
}
|
|
2610
|
+
function verifyContentType(actual, expected) {
|
|
2611
|
+
if (!actual) {
|
|
2612
|
+
throw new HTTPException2(415, {
|
|
2613
|
+
message: "Unsupported Media Type",
|
|
2614
|
+
cause: {
|
|
2615
|
+
code: "api/unsupported-media-type",
|
|
2616
|
+
detail: "Missing content type header"
|
|
2617
|
+
}
|
|
2618
|
+
});
|
|
2619
|
+
}
|
|
2620
|
+
const { type: incomingContentType } = parseContentType(actual);
|
|
2621
|
+
if (incomingContentType !== expected) {
|
|
2622
|
+
throw new HTTPException2(415, {
|
|
2623
|
+
message: "Unsupported Media Type",
|
|
2624
|
+
cause: {
|
|
2625
|
+
code: "api/unsupported-media-type",
|
|
2626
|
+
detail: `Expected content type: ${expected}, but got: ${incomingContentType}`
|
|
2627
|
+
}
|
|
2628
|
+
});
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
async function parseJson(context) {
|
|
2632
|
+
try {
|
|
2633
|
+
return await context.req.json();
|
|
2634
|
+
} catch (error) {
|
|
2635
|
+
throw new HTTPException2(400, {
|
|
2636
|
+
message: "The request body is not valid JSON",
|
|
2637
|
+
cause: {
|
|
2638
|
+
code: "api/invalid-json",
|
|
2639
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
2640
|
+
}
|
|
2641
|
+
});
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
function parseQueryParams(queryParams) {
|
|
2645
|
+
return Object.fromEntries(
|
|
2646
|
+
Object.entries(queryParams).map(([key, value]) => [
|
|
2647
|
+
key,
|
|
2648
|
+
value === "null" ? null : value
|
|
2649
|
+
])
|
|
2650
|
+
);
|
|
2651
|
+
}
|
|
2652
|
+
function parseQueriesParams(queriesParams) {
|
|
2653
|
+
return Object.fromEntries(
|
|
2654
|
+
Object.entries(queriesParams).map(([key, values]) => [
|
|
2655
|
+
key,
|
|
2656
|
+
values.map((value) => value === "null" ? null : value)
|
|
2657
|
+
])
|
|
2658
|
+
);
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
// packages/experimental/src/zukhruf/protocol/session.ts
|
|
2662
|
+
var ZUKHRUF_ROUTE_PREFIX = "/zukhruf/v1";
|
|
2663
|
+
var CREATE_SESSION_ROUTE_PATH = "/session";
|
|
2664
|
+
var SESSION_ROUTE_PATH = "/session/:sessionId";
|
|
2665
|
+
var SESSION_CANCEL_ROUTE_PATH = "/session/:sessionId/cancel";
|
|
2666
|
+
var SESSION_STREAM_ROUTE_PATH = "/session/:sessionId/stream";
|
|
2667
|
+
var INFO_ROUTE_PATH = "/info";
|
|
2668
|
+
var HEALTH_ROUTE_PATH = "/health";
|
|
2669
|
+
var ZUKHRUF_CREATE_SESSION_ROUTE_PATH = `${ZUKHRUF_ROUTE_PREFIX}${CREATE_SESSION_ROUTE_PATH}`;
|
|
2670
|
+
var ZUKHRUF_SESSION_ROUTE_PATH = `${ZUKHRUF_ROUTE_PREFIX}${SESSION_ROUTE_PATH}`;
|
|
2671
|
+
var ZUKHRUF_SESSION_CANCEL_ROUTE_PATH = `${ZUKHRUF_ROUTE_PREFIX}${SESSION_CANCEL_ROUTE_PATH}`;
|
|
2672
|
+
var ZUKHRUF_SESSION_STREAM_ROUTE_PATH = `${ZUKHRUF_ROUTE_PREFIX}${SESSION_STREAM_ROUTE_PATH}`;
|
|
2673
|
+
var ZUKHRUF_INFO_ROUTE_PATH = `${ZUKHRUF_ROUTE_PREFIX}${INFO_ROUTE_PATH}`;
|
|
2674
|
+
var ZUKHRUF_HEALTH_ROUTE_PATH = `${ZUKHRUF_ROUTE_PREFIX}${HEALTH_ROUTE_PATH}`;
|
|
2675
|
+
var ZUKHRUF_SESSION_ID_HEADER = "x-zukhruf-session-id";
|
|
2676
|
+
var MAX_BODY_BYTES = 10 * 1024;
|
|
2677
|
+
var MAX_IDEMPOTENCY_KEY_LENGTH = 200;
|
|
2678
|
+
var MAX_INPUT_LENGTH = 8e3;
|
|
2679
|
+
var SESSION_NAMESPACE = uuidv56("urn:deepagents:zukhruf:sessions", uuidv56.URL);
|
|
2680
|
+
var NO_STORE = { "cache-control": "no-store" };
|
|
2681
|
+
var sessionIdSchema = z9.string().refine(validateUuid);
|
|
2682
|
+
var turnBodySchema = z9.strictObject({
|
|
2683
|
+
input: z9.string().trim().min(1).max(MAX_INPUT_LENGTH)
|
|
2684
|
+
});
|
|
2685
|
+
var idempotencyKeySchema = z9.string().trim().min(1).max(MAX_IDEMPOTENCY_KEY_LENGTH);
|
|
2686
|
+
var limitTurnBody = bodyLimit({
|
|
2687
|
+
maxSize: MAX_BODY_BYTES,
|
|
2688
|
+
onError: () => {
|
|
2689
|
+
throw new HTTPException3(413, {
|
|
2690
|
+
message: "Request body is too large",
|
|
2691
|
+
cause: {
|
|
2692
|
+
code: "api/payload-too-large",
|
|
2693
|
+
detail: `Request body exceeds ${MAX_BODY_BYTES} bytes`
|
|
2694
|
+
}
|
|
2695
|
+
});
|
|
2696
|
+
}
|
|
2697
|
+
});
|
|
2698
|
+
function zukhruf(runtime) {
|
|
2699
|
+
const app = new Hono();
|
|
2700
|
+
app.onError((error, context) => {
|
|
2701
|
+
if (error instanceof HTTPException3) {
|
|
2702
|
+
return context.json(
|
|
2703
|
+
{ cause: error.cause, error: error.message },
|
|
2704
|
+
error.status,
|
|
2705
|
+
NO_STORE
|
|
2706
|
+
);
|
|
2707
|
+
}
|
|
2708
|
+
throw error;
|
|
2709
|
+
});
|
|
2710
|
+
app.on(
|
|
2711
|
+
["GET", "HEAD"],
|
|
2712
|
+
HEALTH_ROUTE_PATH,
|
|
2713
|
+
(context) => context.req.method === "HEAD" ? context.body(null, 200, NO_STORE) : context.json({ ok: true }, 200, NO_STORE)
|
|
2714
|
+
);
|
|
2715
|
+
app.all(
|
|
2716
|
+
HEALTH_ROUTE_PATH,
|
|
2717
|
+
(context) => methodNotAllowed(context, "GET, HEAD")
|
|
2718
|
+
);
|
|
2719
|
+
app.use("*", async (context, next) => {
|
|
2720
|
+
if (!context.get("userId")?.trim()) {
|
|
2721
|
+
throw new HTTPException3(401, {
|
|
2722
|
+
message: "Authentication required",
|
|
2723
|
+
cause: {
|
|
2724
|
+
code: "api/unauthenticated",
|
|
2725
|
+
detail: "Authentication required to access Zukhruf"
|
|
2726
|
+
}
|
|
2727
|
+
});
|
|
2728
|
+
}
|
|
2729
|
+
await next();
|
|
2730
|
+
});
|
|
2731
|
+
app.get(
|
|
2732
|
+
INFO_ROUTE_PATH,
|
|
2733
|
+
(context) => context.json(runtime.info, 200, NO_STORE)
|
|
2734
|
+
);
|
|
2735
|
+
app.all(INFO_ROUTE_PATH, (context) => methodNotAllowed(context, "GET"));
|
|
2736
|
+
app.post(
|
|
2737
|
+
CREATE_SESSION_ROUTE_PATH,
|
|
2738
|
+
limitTurnBody,
|
|
2739
|
+
validate("application/json", (payload) => ({
|
|
2740
|
+
body: {
|
|
2741
|
+
select: payload.body,
|
|
2742
|
+
against: turnBodySchema
|
|
2743
|
+
},
|
|
2744
|
+
idempotencyKey: {
|
|
2745
|
+
select: payload.headers["idempotency-key"],
|
|
2746
|
+
against: idempotencyKeySchema
|
|
2747
|
+
}
|
|
2748
|
+
})),
|
|
2749
|
+
async (context) => {
|
|
2750
|
+
const { body, idempotencyKey } = context.var.input;
|
|
2751
|
+
const userId = context.get("userId");
|
|
2752
|
+
const sessionId = uuidv56(
|
|
2753
|
+
JSON.stringify([userId, idempotencyKey]),
|
|
2754
|
+
SESSION_NAMESPACE
|
|
2755
|
+
);
|
|
2756
|
+
const conversation = { chatId: sessionId, userId };
|
|
2757
|
+
await runtime.createSession(conversation);
|
|
2758
|
+
await runtime.enqueue(conversation, {
|
|
2759
|
+
id: idempotencyKey,
|
|
2760
|
+
input: body.input.trim()
|
|
2761
|
+
});
|
|
2762
|
+
return accepted(context, sessionId);
|
|
2763
|
+
}
|
|
2764
|
+
);
|
|
2765
|
+
app.all(
|
|
2766
|
+
CREATE_SESSION_ROUTE_PATH,
|
|
2767
|
+
(context) => methodNotAllowed(context, "POST")
|
|
2768
|
+
);
|
|
2769
|
+
app.post(
|
|
2770
|
+
SESSION_ROUTE_PATH,
|
|
2771
|
+
limitTurnBody,
|
|
2772
|
+
validate("application/json", (payload) => ({
|
|
2773
|
+
body: {
|
|
2774
|
+
select: payload.body,
|
|
2775
|
+
against: turnBodySchema
|
|
2776
|
+
},
|
|
2777
|
+
idempotencyKey: {
|
|
2778
|
+
select: payload.headers["idempotency-key"],
|
|
2779
|
+
against: idempotencyKeySchema
|
|
2780
|
+
},
|
|
2781
|
+
sessionId: {
|
|
2782
|
+
select: payload.params.sessionId,
|
|
2783
|
+
against: sessionIdSchema
|
|
2784
|
+
}
|
|
2785
|
+
})),
|
|
2786
|
+
async (context) => {
|
|
2787
|
+
const { body, idempotencyKey, sessionId } = context.var.input;
|
|
2788
|
+
const conversation = {
|
|
2789
|
+
chatId: sessionId,
|
|
2790
|
+
userId: context.get("userId")
|
|
2791
|
+
};
|
|
2792
|
+
await requireSession(runtime, conversation);
|
|
2793
|
+
await runtime.enqueue(conversation, {
|
|
2794
|
+
id: idempotencyKey,
|
|
2795
|
+
input: body.input.trim()
|
|
2796
|
+
});
|
|
2797
|
+
return accepted(context, sessionId);
|
|
2798
|
+
}
|
|
2799
|
+
);
|
|
2800
|
+
app.all(SESSION_ROUTE_PATH, (context) => methodNotAllowed(context, "POST"));
|
|
2801
|
+
app.post(
|
|
2802
|
+
SESSION_CANCEL_ROUTE_PATH,
|
|
2803
|
+
validate((payload) => ({
|
|
2804
|
+
sessionId: {
|
|
2805
|
+
select: payload.params.sessionId,
|
|
2806
|
+
against: sessionIdSchema
|
|
2807
|
+
}
|
|
2808
|
+
})),
|
|
2809
|
+
async (context) => {
|
|
2810
|
+
const { sessionId } = context.var.input;
|
|
2811
|
+
const conversation = {
|
|
2812
|
+
chatId: sessionId,
|
|
2813
|
+
userId: context.get("userId")
|
|
2814
|
+
};
|
|
2815
|
+
await requireSession(runtime, conversation);
|
|
2816
|
+
await runtime.observe(conversation).cancel();
|
|
2817
|
+
return context.body(null, 204, NO_STORE);
|
|
2818
|
+
}
|
|
2819
|
+
);
|
|
2820
|
+
app.all(
|
|
2821
|
+
SESSION_CANCEL_ROUTE_PATH,
|
|
2822
|
+
(context) => methodNotAllowed(context, "POST")
|
|
2823
|
+
);
|
|
2824
|
+
app.get(
|
|
2825
|
+
SESSION_STREAM_ROUTE_PATH,
|
|
2826
|
+
validate((payload) => ({
|
|
2827
|
+
sessionId: {
|
|
2828
|
+
select: payload.params.sessionId,
|
|
2829
|
+
against: sessionIdSchema
|
|
2830
|
+
}
|
|
2831
|
+
})),
|
|
2832
|
+
async (context) => {
|
|
2833
|
+
const { sessionId } = context.var.input;
|
|
2834
|
+
const conversation = {
|
|
2835
|
+
chatId: sessionId,
|
|
2836
|
+
userId: context.get("userId")
|
|
2837
|
+
};
|
|
2838
|
+
await requireSession(runtime, conversation);
|
|
2839
|
+
const stream = await runtime.observe(conversation).resume();
|
|
2840
|
+
if (!stream) {
|
|
2841
|
+
throw new HTTPException3(404, {
|
|
2842
|
+
message: "Session stream not found",
|
|
2843
|
+
cause: {
|
|
2844
|
+
code: "zukhruf/session-stream-not-found",
|
|
2845
|
+
detail: `No durable stream exists for session ${sessionId}`
|
|
2846
|
+
}
|
|
2847
|
+
});
|
|
2848
|
+
}
|
|
2849
|
+
return createUIMessageStreamResponse({
|
|
2850
|
+
headers: NO_STORE,
|
|
2851
|
+
stream
|
|
2852
|
+
});
|
|
2853
|
+
}
|
|
2854
|
+
);
|
|
2855
|
+
app.all(
|
|
2856
|
+
SESSION_STREAM_ROUTE_PATH,
|
|
2857
|
+
(context) => methodNotAllowed(context, "GET")
|
|
2858
|
+
);
|
|
2859
|
+
return app;
|
|
2860
|
+
}
|
|
2861
|
+
function accepted(context, sessionId) {
|
|
2862
|
+
return context.json({ ok: true, sessionId }, 202, {
|
|
2863
|
+
...NO_STORE,
|
|
2864
|
+
[ZUKHRUF_SESSION_ID_HEADER]: sessionId
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
async function requireSession(runtime, conversation) {
|
|
2868
|
+
if (await runtime.sessionExists(conversation)) return;
|
|
2869
|
+
throw new HTTPException3(404, {
|
|
2870
|
+
message: "Session not found",
|
|
2871
|
+
cause: {
|
|
2872
|
+
code: "zukhruf/session-not-found",
|
|
2873
|
+
detail: `Session ${conversation.chatId} does not exist`
|
|
2874
|
+
}
|
|
2875
|
+
});
|
|
2876
|
+
}
|
|
2877
|
+
function methodNotAllowed(context, allow) {
|
|
2878
|
+
context.header("Allow", allow);
|
|
2879
|
+
throw new HTTPException3(405, {
|
|
2880
|
+
message: "Method not allowed",
|
|
2881
|
+
cause: {
|
|
2882
|
+
code: "api/method-not-allowed",
|
|
2883
|
+
detail: `This endpoint only accepts ${allow} requests`
|
|
2884
|
+
}
|
|
2885
|
+
});
|
|
2886
|
+
}
|
|
2887
|
+
|
|
2475
2888
|
// packages/experimental/src/zukhruf/queue/pg-boss.turn-queue.ts
|
|
2476
2889
|
import { v7 as uuidv7 } from "uuid";
|
|
2477
2890
|
|
|
@@ -2871,12 +3284,21 @@ export {
|
|
|
2871
3284
|
PgBossTurnQueue,
|
|
2872
3285
|
SqliteMailboxStore,
|
|
2873
3286
|
TurnQueue,
|
|
3287
|
+
ZUKHRUF_CREATE_SESSION_ROUTE_PATH,
|
|
3288
|
+
ZUKHRUF_HEALTH_ROUTE_PATH,
|
|
3289
|
+
ZUKHRUF_INFO_ROUTE_PATH,
|
|
3290
|
+
ZUKHRUF_ROUTE_PREFIX,
|
|
3291
|
+
ZUKHRUF_SESSION_CANCEL_ROUTE_PATH,
|
|
3292
|
+
ZUKHRUF_SESSION_ID_HEADER,
|
|
3293
|
+
ZUKHRUF_SESSION_ROUTE_PATH,
|
|
3294
|
+
ZUKHRUF_SESSION_STREAM_ROUTE_PATH,
|
|
2874
3295
|
assertCommunication,
|
|
2875
3296
|
createInterAgentCommunication,
|
|
2876
3297
|
defineAgent,
|
|
2877
3298
|
defineInstructions,
|
|
2878
3299
|
defineSandbox,
|
|
2879
3300
|
defineTool,
|
|
2880
|
-
renderTurn
|
|
3301
|
+
renderTurn,
|
|
3302
|
+
zukhruf
|
|
2881
3303
|
};
|
|
2882
3304
|
//# sourceMappingURL=index.js.map
|