@deepagents/experimental 6.1.2 → 7.0.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/README.md +151 -10
- package/dist/coding-agent-reminders/types.d.ts.map +1 -1
- package/dist/index.js +3975 -2519
- package/dist/index.js.map +4 -4
- package/dist/providers/claude/index.d.ts +4 -0
- package/dist/providers/claude/index.d.ts.map +1 -0
- package/dist/providers/claude/index.js +196 -0
- package/dist/providers/claude/index.js.map +7 -0
- package/dist/providers/claude/local-credentials.d.ts +3 -0
- package/dist/providers/claude/local-credentials.d.ts.map +1 -0
- package/dist/providers/codex/generate-via-stream.d.ts +8 -0
- package/dist/providers/codex/generate-via-stream.d.ts.map +1 -0
- package/dist/providers/codex/index.d.ts +4 -0
- package/dist/providers/codex/index.d.ts.map +1 -0
- package/dist/providers/codex/index.js +358 -0
- package/dist/providers/codex/index.js.map +7 -0
- package/dist/providers/codex/local-credentials.d.ts +4 -0
- package/dist/providers/codex/local-credentials.d.ts.map +1 -0
- package/dist/providers/language-model-provider.d.ts +5 -0
- package/dist/providers/language-model-provider.d.ts.map +1 -0
- package/dist/zukhruf/agent.d.ts +17 -2
- package/dist/zukhruf/agent.d.ts.map +1 -1
- package/dist/zukhruf/collaboration/agent-tool-context.d.ts +2 -2
- package/dist/zukhruf/collaboration/agent-tool-context.d.ts.map +1 -1
- package/dist/zukhruf/collaboration/collaboration-tools.d.ts +1807 -3
- package/dist/zukhruf/collaboration/collaboration-tools.d.ts.map +1 -1
- package/dist/zukhruf/collaboration/spawn-agent.d.ts.map +1 -1
- package/dist/zukhruf/collaboration/wait-agent.d.ts.map +1 -1
- package/dist/zukhruf/control-plane/agent-control-plane.d.ts +6 -7
- package/dist/zukhruf/control-plane/agent-control-plane.d.ts.map +1 -1
- package/dist/zukhruf/control-plane/agent-status-projector.d.ts +6 -3
- package/dist/zukhruf/control-plane/agent-status-projector.d.ts.map +1 -1
- package/dist/zukhruf/control-plane/agent-thread.d.ts.map +1 -1
- package/dist/zukhruf/index.d.ts +7 -4
- package/dist/zukhruf/index.d.ts.map +1 -1
- package/dist/zukhruf/index.js +4184 -2728
- package/dist/zukhruf/index.js.map +4 -4
- package/dist/zukhruf/{multi-agent-v2-config.d.ts → multi-agent-config.d.ts} +14 -9
- package/dist/zukhruf/multi-agent-config.d.ts.map +1 -0
- package/dist/zukhruf/plugins/conversation-scheduling/conversation-scheduler.d.ts +76 -0
- package/dist/zukhruf/plugins/conversation-scheduling/conversation-scheduler.d.ts.map +1 -0
- package/dist/zukhruf/plugins/conversation-scheduling/index.d.ts +13 -0
- package/dist/zukhruf/plugins/conversation-scheduling/index.d.ts.map +1 -0
- package/dist/zukhruf/plugins/conversation-scheduling/index.js +999 -0
- package/dist/zukhruf/plugins/conversation-scheduling/index.js.map +7 -0
- package/dist/zukhruf/plugins/conversation-scheduling/pg-boss.wake-scheduler.d.ts +19 -0
- package/dist/zukhruf/plugins/conversation-scheduling/pg-boss.wake-scheduler.d.ts.map +1 -0
- package/dist/zukhruf/plugins/conversation-scheduling/tools.d.ts +1305 -0
- package/dist/zukhruf/plugins/conversation-scheduling/tools.d.ts.map +1 -0
- package/dist/zukhruf/plugins/conversation-scheduling/wake-scheduler.d.ts +12 -0
- package/dist/zukhruf/plugins/conversation-scheduling/wake-scheduler.d.ts.map +1 -0
- package/dist/zukhruf/plugins/http/index.d.ts +50 -0
- package/dist/zukhruf/plugins/http/index.d.ts.map +1 -0
- package/dist/zukhruf/plugins/http/index.js +618 -0
- package/dist/zukhruf/plugins/http/index.js.map +7 -0
- package/dist/zukhruf/plugins/http/parse.d.ts.map +1 -0
- package/dist/zukhruf/plugins/http/validator.d.ts.map +1 -0
- package/dist/zukhruf/plugins/mcp/index.d.ts +8 -0
- package/dist/zukhruf/plugins/mcp/index.d.ts.map +1 -0
- package/dist/zukhruf/plugins/mcp/index.js +25 -0
- package/dist/zukhruf/plugins/mcp/index.js.map +7 -0
- package/dist/zukhruf/plugins/schedules/http.d.ts +45 -0
- package/dist/zukhruf/plugins/schedules/http.d.ts.map +1 -0
- package/dist/zukhruf/plugins/schedules/http.js +528 -0
- package/dist/zukhruf/plugins/schedules/http.js.map +7 -0
- package/dist/zukhruf/plugins/schedules/index.d.ts +35 -0
- package/dist/zukhruf/plugins/schedules/index.d.ts.map +1 -0
- package/dist/zukhruf/plugins/schedules/index.js +1873 -0
- package/dist/zukhruf/plugins/schedules/index.js.map +7 -0
- package/dist/zukhruf/plugins/schedules/schedule-files.d.ts +6 -0
- package/dist/zukhruf/plugins/schedules/schedule-files.d.ts.map +1 -0
- package/dist/zukhruf/plugins/schedules/scheduled-tasks.d.ts +157 -0
- package/dist/zukhruf/plugins/schedules/scheduled-tasks.d.ts.map +1 -0
- package/dist/zukhruf/plugins/uploads/http.d.ts +27 -0
- package/dist/zukhruf/plugins/uploads/http.d.ts.map +1 -0
- package/dist/zukhruf/plugins/uploads/http.js +426 -0
- package/dist/zukhruf/plugins/uploads/http.js.map +7 -0
- package/dist/zukhruf/plugins/uploads/index.d.ts +73 -0
- package/dist/zukhruf/plugins/uploads/index.d.ts.map +1 -0
- package/dist/zukhruf/plugins/uploads/index.js +247 -0
- package/dist/zukhruf/plugins/uploads/index.js.map +7 -0
- package/dist/zukhruf/queue/pg-boss-notifications.d.ts +10 -0
- package/dist/zukhruf/queue/pg-boss-notifications.d.ts.map +1 -0
- package/dist/zukhruf/queue/pg-boss.turn-queue.d.ts +8 -11
- package/dist/zukhruf/queue/pg-boss.turn-queue.d.ts.map +1 -1
- package/dist/zukhruf/queue/turn-queue.d.ts +43 -38
- package/dist/zukhruf/queue/turn-queue.d.ts.map +1 -1
- package/dist/zukhruf/runtime/agent-runtime.d.ts +148 -43
- package/dist/zukhruf/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/zukhruf/runtime/agent-turn-executor.d.ts +9 -2
- package/dist/zukhruf/runtime/agent-turn-executor.d.ts.map +1 -1
- package/dist/zukhruf/runtime/approval-controller.d.ts +10 -25
- package/dist/zukhruf/runtime/approval-controller.d.ts.map +1 -1
- package/dist/zukhruf/runtime/compaction.d.ts +47 -0
- package/dist/zukhruf/runtime/compaction.d.ts.map +1 -0
- package/dist/zukhruf/runtime/conversation-status/change-source.d.ts +19 -0
- package/dist/zukhruf/runtime/conversation-status/change-source.d.ts.map +1 -0
- package/dist/zukhruf/runtime/conversation-status/child-progress.d.ts +84 -0
- package/dist/zukhruf/runtime/conversation-status/child-progress.d.ts.map +1 -0
- package/dist/zukhruf/runtime/conversation-status/pg-boss-change-source.d.ts +22 -0
- package/dist/zukhruf/runtime/conversation-status/pg-boss-change-source.d.ts.map +1 -0
- package/dist/zukhruf/runtime/conversation-status/projector.d.ts +62 -0
- package/dist/zukhruf/runtime/conversation-status/projector.d.ts.map +1 -0
- package/dist/zukhruf/runtime/conversation-status/status-publishing-turn-queue.d.ts +20 -0
- package/dist/zukhruf/runtime/conversation-status/status-publishing-turn-queue.d.ts.map +1 -0
- package/dist/zukhruf/runtime/plugin/agent-skills.d.ts +22 -0
- package/dist/zukhruf/runtime/plugin/agent-skills.d.ts.map +1 -0
- package/dist/zukhruf/runtime/plugin/plugin-agents.d.ts +14 -0
- package/dist/zukhruf/runtime/plugin/plugin-agents.d.ts.map +1 -0
- package/dist/zukhruf/runtime/plugin/plugin-bindings.d.ts +8 -0
- package/dist/zukhruf/runtime/plugin/plugin-bindings.d.ts.map +1 -0
- package/dist/zukhruf/runtime/plugin/plugin-context-composition.d.ts +7 -0
- package/dist/zukhruf/runtime/plugin/plugin-context-composition.d.ts.map +1 -0
- package/dist/zukhruf/runtime/plugin/plugin-manager.d.ts +38 -0
- package/dist/zukhruf/runtime/plugin/plugin-manager.d.ts.map +1 -0
- package/dist/zukhruf/runtime/plugin/plugin-tool-composition.d.ts +10 -0
- package/dist/zukhruf/runtime/plugin/plugin-tool-composition.d.ts.map +1 -0
- package/dist/zukhruf/tool.d.ts +5 -1
- package/dist/zukhruf/tool.d.ts.map +1 -1
- package/package.json +72 -6
- package/dist/zukhruf/multi-agent-v2-config.d.ts.map +0 -1
- package/dist/zukhruf/protocol/parse.d.ts.map +0 -1
- package/dist/zukhruf/protocol/session.d.ts +0 -23
- package/dist/zukhruf/protocol/session.d.ts.map +0 -1
- package/dist/zukhruf/protocol/validator.d.ts.map +0 -1
- package/dist/zukhruf/runtime/agent-skills.d.ts +0 -9
- package/dist/zukhruf/runtime/agent-skills.d.ts.map +0 -1
- package/dist/zukhruf/runtime/approval-job-id.d.ts +0 -3
- package/dist/zukhruf/runtime/approval-job-id.d.ts.map +0 -1
- /package/dist/zukhruf/{protocol → plugins/http}/parse.d.ts +0 -0
- /package/dist/zukhruf/{protocol → plugins/http}/validator.d.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../../../src/zukhruf/plugins/conversation-scheduling/tools.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAC7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAyFzE,wBAAgB,qBAAqB,CACnC,qBAAqB,EAAE,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAiG7C"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface Wake<T extends object> {
|
|
2
|
+
id: string;
|
|
3
|
+
runAt: Date;
|
|
4
|
+
data: T;
|
|
5
|
+
}
|
|
6
|
+
/** Durable, at-least-once delivery of opaque one-shot wakes. */
|
|
7
|
+
export declare abstract class WakeScheduler<T extends object> {
|
|
8
|
+
abstract schedule(wake: Wake<T>): Promise<void>;
|
|
9
|
+
abstract cancel(id: string): Promise<void>;
|
|
10
|
+
abstract consume(handler: (wake: Wake<T>) => Promise<void>, waitForActive?: boolean): Promise<AsyncDisposable>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=wake-scheduler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wake-scheduler.d.ts","sourceRoot":"","sources":["../../../../src/zukhruf/plugins/conversation-scheduling/wake-scheduler.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,IAAI,CAAC,CAAC,SAAS,MAAM;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,IAAI,CAAC;IACZ,IAAI,EAAE,CAAC,CAAC;CACT;AAED,gEAAgE;AAChE,8BAAsB,aAAa,CAAC,CAAC,SAAS,MAAM;IAClD,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAC/C,QAAQ,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAC1C,QAAQ,CAAC,OAAO,CACd,OAAO,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,EACzC,aAAa,CAAC,EAAE,OAAO,GACtB,OAAO,CAAC,eAAe,CAAC;CAC5B"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Context, Hono } from 'hono';
|
|
2
|
+
import type { ConversationId } from '../../mailbox/types.ts';
|
|
3
|
+
import type { AgentHost, AgentObservation, AgentPluginDefinition, AgentPluginInstance } from '../../runtime/agent-runtime.ts';
|
|
4
|
+
export type HttpEnv = {
|
|
5
|
+
Variables: {
|
|
6
|
+
userId: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export type OwnerEvent = {
|
|
10
|
+
readonly type: 'ready';
|
|
11
|
+
} | {
|
|
12
|
+
readonly type: 'change';
|
|
13
|
+
readonly resource: string;
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly [key: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
type OwnerEventNotification = Exclude<OwnerEvent, {
|
|
18
|
+
type: 'ready';
|
|
19
|
+
}> | {
|
|
20
|
+
readonly type: 'reset';
|
|
21
|
+
};
|
|
22
|
+
export type OwnerEventSource = (userId: string, signal: AbortSignal) => Promise<AsyncIterable<OwnerEventNotification>>;
|
|
23
|
+
/** A discoverable feature: its route under the mount plus any detail the client needs to use it. */
|
|
24
|
+
export interface HttpCapability {
|
|
25
|
+
readonly path: string;
|
|
26
|
+
readonly [detail: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
export interface HttpContribution {
|
|
29
|
+
readonly capabilities: Readonly<Record<string, HttpCapability>>;
|
|
30
|
+
readonly publicRoutes?: Hono<HttpEnv>;
|
|
31
|
+
readonly authenticatedRoutes?: Hono<HttpEnv>;
|
|
32
|
+
readonly events?: OwnerEventSource;
|
|
33
|
+
}
|
|
34
|
+
export interface HttpProjection {
|
|
35
|
+
project(runtime: HttpRuntime): HttpContribution;
|
|
36
|
+
}
|
|
37
|
+
export declare function projectHttp<Instance extends object>(definition: AgentPluginDefinition<Instance>, project: (plugin: AgentPluginInstance & Instance, runtime: HttpRuntime) => HttpContribution): HttpProjection;
|
|
38
|
+
export declare const ZUKHRUF_SESSION_ID_HEADER = "x-zukhruf-session-id";
|
|
39
|
+
export declare const ZUKHRUF_TURN_ID_HEADER = "x-zukhruf-turn-id";
|
|
40
|
+
/** Where the host mounted this API, without a trailing slash; empty at `/`. */
|
|
41
|
+
export declare function mountPath(context: Context): string;
|
|
42
|
+
export interface HttpRuntime extends Pick<AgentHost, 'createSession' | 'enqueue' | 'info' | 'listHistory' | 'plugin' | 'sessionExists' | 'subscribeConversationStatus'> {
|
|
43
|
+
observe(conversation: ConversationId): Pick<AgentObservation, 'cancel' | 'resume' | 'status'> & {
|
|
44
|
+
engine: Pick<AgentObservation['engine'], 'getMessages'>;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Mount at the host-selected path with `app.route(path, http(runtime))`. */
|
|
48
|
+
export declare function http(runtime: HttpRuntime, ...projections: readonly HttpProjection[]): Hono<HttpEnv, import("hono/types").BlankSchema, "/">;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/zukhruf/plugins/http/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAS1C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACpB,MAAM,gCAAgC,CAAC;AAGxC,MAAM,MAAM,OAAO,GAAG;IAAE,SAAS,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC;AAExD,MAAM,MAAM,UAAU,GAClB;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAC1B;IACE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACjC,CAAC;AAEN,KAAK,sBAAsB,GACzB,OAAO,CAAC,UAAU,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAEtE,MAAM,MAAM,gBAAgB,GAAG,CAC7B,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,WAAW,KAChB,OAAO,CAAC,aAAa,CAAC,sBAAsB,CAAC,CAAC,CAAC;AAEpD,oGAAoG;AACpG,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;CACpC;AAED,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC;IAChE,QAAQ,CAAC,YAAY,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IACtC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IAC7C,QAAQ,CAAC,MAAM,CAAC,EAAE,gBAAgB,CAAC;CACpC;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,gBAAgB,CAAC;CACjD;AAED,wBAAgB,WAAW,CAAC,QAAQ,SAAS,MAAM,EACjD,UAAU,EAAE,qBAAqB,CAAC,QAAQ,CAAC,EAC3C,OAAO,EAAE,CACP,MAAM,EAAE,mBAAmB,GAAG,QAAQ,EACtC,OAAO,EAAE,WAAW,KACjB,gBAAgB,GACpB,cAAc,CAMhB;AAYD,eAAO,MAAM,yBAAyB,yBAAyB,CAAC;AAChE,eAAO,MAAM,sBAAsB,sBAAsB,CAAC;AAE1D,+EAA+E;AAC/E,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAElD;AAyBD,MAAM,WAAW,WAAY,SAAQ,IAAI,CACvC,SAAS,EACP,eAAe,GACf,SAAS,GACT,MAAM,GACN,aAAa,GACb,QAAQ,GACR,eAAe,GACf,6BAA6B,CAChC;IACC,OAAO,CAAC,YAAY,EAAE,cAAc,GAAG,IAAI,CACzC,gBAAgB,EAChB,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAC/B,GAAG;QACF,MAAM,EAAE,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAC,CAAC;KACzD,CAAC;CACH;AAED,6EAA6E;AAC7E,wBAAgB,IAAI,CAClB,OAAO,EAAE,WAAW,EACpB,GAAG,WAAW,EAAE,SAAS,cAAc,EAAE,wDAmW1C"}
|
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
// packages/experimental/src/zukhruf/plugins/http/index.ts
|
|
2
|
+
import {
|
|
3
|
+
createUIMessageStreamResponse,
|
|
4
|
+
safeValidateUIMessages
|
|
5
|
+
} from "ai";
|
|
6
|
+
import { Hono } from "hono";
|
|
7
|
+
import { HTTPException as HTTPException3 } from "hono/http-exception";
|
|
8
|
+
import { basePath } from "hono/route";
|
|
9
|
+
import { streamSSE } from "hono/streaming";
|
|
10
|
+
import { validate as validateUuid } from "uuid";
|
|
11
|
+
import z3 from "zod";
|
|
12
|
+
import { elementsSchema } from "@deepagents/elements";
|
|
13
|
+
|
|
14
|
+
// packages/experimental/src/zukhruf/plugins/http/validator.ts
|
|
15
|
+
import { parse as parseContentType } from "fast-content-type-parse";
|
|
16
|
+
import { createMiddleware } from "hono/factory";
|
|
17
|
+
import { HTTPException as HTTPException2 } from "hono/http-exception";
|
|
18
|
+
import z2 from "zod";
|
|
19
|
+
|
|
20
|
+
// packages/experimental/src/zukhruf/plugins/http/parse.ts
|
|
21
|
+
import { HTTPException } from "hono/http-exception";
|
|
22
|
+
import "zod";
|
|
23
|
+
async function parse(schema, input) {
|
|
24
|
+
const result = await schema.safeParseAsync(input);
|
|
25
|
+
if (!result.success) {
|
|
26
|
+
const errors = result.error.flatten((issue) => ({
|
|
27
|
+
message: issue.message,
|
|
28
|
+
code: issue.code,
|
|
29
|
+
path: issue.path.join(".")
|
|
30
|
+
})).fieldErrors;
|
|
31
|
+
throw new HTTPException(400, {
|
|
32
|
+
message: "Validation failed",
|
|
33
|
+
cause: {
|
|
34
|
+
code: "api/validation-failed",
|
|
35
|
+
detail: "The input data is invalid",
|
|
36
|
+
errors
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return result.data;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// packages/experimental/src/zukhruf/plugins/http/validator.ts
|
|
44
|
+
function validate(expectedContentTypeOrSelector, selector) {
|
|
45
|
+
const expectedContentType = typeof expectedContentTypeOrSelector === "string" ? expectedContentTypeOrSelector : void 0;
|
|
46
|
+
const _selector = typeof expectedContentTypeOrSelector === "function" ? expectedContentTypeOrSelector : selector;
|
|
47
|
+
void _selector;
|
|
48
|
+
if (!_selector) {
|
|
49
|
+
throw new Error("Selector function is required");
|
|
50
|
+
}
|
|
51
|
+
return createMiddleware(async (c, next) => {
|
|
52
|
+
const ct = c.req.header("content-type");
|
|
53
|
+
if (c.req.method === "GET" && ct) {
|
|
54
|
+
throw new HTTPException2(415, {
|
|
55
|
+
message: "Unsupported Media Type",
|
|
56
|
+
cause: {
|
|
57
|
+
code: "api/unsupported-media-type",
|
|
58
|
+
detail: `GET requests cannot have a content type header`
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (expectedContentType) {
|
|
63
|
+
void verifyContentType(ct, expectedContentType);
|
|
64
|
+
}
|
|
65
|
+
const contentType = ct ? parseContentType(ct) : null;
|
|
66
|
+
let body = null;
|
|
67
|
+
switch (contentType?.type) {
|
|
68
|
+
case "application/json":
|
|
69
|
+
body = await parseJson(c);
|
|
70
|
+
break;
|
|
71
|
+
case "application/x-www-form-urlencoded":
|
|
72
|
+
case "multipart/form-data":
|
|
73
|
+
body = await c.req.parseBody();
|
|
74
|
+
break;
|
|
75
|
+
default:
|
|
76
|
+
body = {};
|
|
77
|
+
}
|
|
78
|
+
const payload = {
|
|
79
|
+
body,
|
|
80
|
+
query: parseQueryParams(c.req.query()),
|
|
81
|
+
queries: parseQueriesParams(c.req.queries()),
|
|
82
|
+
params: c.req.param(),
|
|
83
|
+
headers: c.req.header()
|
|
84
|
+
};
|
|
85
|
+
const config = _selector(payload);
|
|
86
|
+
const schema = z2.object(
|
|
87
|
+
Object.entries(config).reduce(
|
|
88
|
+
(acc, [key, value]) => {
|
|
89
|
+
acc[key] = value.against;
|
|
90
|
+
return acc;
|
|
91
|
+
},
|
|
92
|
+
{}
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
const input = Object.entries(config).reduce(
|
|
96
|
+
(acc, [key, value]) => {
|
|
97
|
+
acc[key] = value.select;
|
|
98
|
+
return acc;
|
|
99
|
+
},
|
|
100
|
+
{}
|
|
101
|
+
);
|
|
102
|
+
const parsed = await parse(schema, input);
|
|
103
|
+
c.set("input", parsed);
|
|
104
|
+
await next();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function verifyContentType(actual, expected) {
|
|
108
|
+
if (!actual) {
|
|
109
|
+
throw new HTTPException2(415, {
|
|
110
|
+
message: "Unsupported Media Type",
|
|
111
|
+
cause: {
|
|
112
|
+
code: "api/unsupported-media-type",
|
|
113
|
+
detail: "Missing content type header"
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const { type: incomingContentType } = parseContentType(actual);
|
|
118
|
+
if (incomingContentType !== expected) {
|
|
119
|
+
throw new HTTPException2(415, {
|
|
120
|
+
message: "Unsupported Media Type",
|
|
121
|
+
cause: {
|
|
122
|
+
code: "api/unsupported-media-type",
|
|
123
|
+
detail: `Expected content type: ${expected}, but got: ${incomingContentType}`
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function parseJson(context) {
|
|
129
|
+
try {
|
|
130
|
+
return await context.req.json();
|
|
131
|
+
} catch (error) {
|
|
132
|
+
throw new HTTPException2(400, {
|
|
133
|
+
message: "The request body is not valid JSON",
|
|
134
|
+
cause: {
|
|
135
|
+
code: "api/invalid-json",
|
|
136
|
+
detail: error instanceof Error ? error.message : String(error)
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function parseQueryParams(queryParams) {
|
|
142
|
+
return Object.fromEntries(
|
|
143
|
+
Object.entries(queryParams).map(([key, value]) => [
|
|
144
|
+
key,
|
|
145
|
+
value === "null" ? null : value
|
|
146
|
+
])
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
function parseQueriesParams(queriesParams) {
|
|
150
|
+
return Object.fromEntries(
|
|
151
|
+
Object.entries(queriesParams).map(([key, values]) => [
|
|
152
|
+
key,
|
|
153
|
+
values.map((value) => value === "null" ? null : value)
|
|
154
|
+
])
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// packages/experimental/src/zukhruf/plugins/http/index.ts
|
|
159
|
+
function projectHttp(definition, project) {
|
|
160
|
+
return {
|
|
161
|
+
project(runtime) {
|
|
162
|
+
return project(runtime.plugin(definition), runtime);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
var SESSION_ROUTE_PATH = "/session/:sessionId";
|
|
167
|
+
var SESSION_CANCEL_ROUTE_PATH = "/session/:sessionId/cancel";
|
|
168
|
+
var SESSION_STREAM_ROUTE_PATH = "/session/:sessionId/stream";
|
|
169
|
+
var EVENTS_ROUTE_PATH = "/events";
|
|
170
|
+
var SESSION_TURN_ROUTE_PATH = "/session/:sessionId/turn/:turnId";
|
|
171
|
+
var SESSION_TURN_CANCEL_ROUTE_PATH = "/session/:sessionId/turn/:turnId/cancel";
|
|
172
|
+
var HISTORY_ROUTE_PATH = "/history";
|
|
173
|
+
var INFO_ROUTE_PATH = "/info";
|
|
174
|
+
var HEALTH_ROUTE_PATH = "/health";
|
|
175
|
+
var ZUKHRUF_SESSION_ID_HEADER = "x-zukhruf-session-id";
|
|
176
|
+
var ZUKHRUF_TURN_ID_HEADER = "x-zukhruf-turn-id";
|
|
177
|
+
function mountPath(context) {
|
|
178
|
+
return basePath(context).replace(/\/$/, "");
|
|
179
|
+
}
|
|
180
|
+
var NO_STORE = { "cache-control": "no-store" };
|
|
181
|
+
var sessionIdSchema = z3.string().refine(validateUuid);
|
|
182
|
+
var jsonSchema = z3.custom(
|
|
183
|
+
(value) => typeof value === "object" && value !== null && !Array.isArray(value)
|
|
184
|
+
);
|
|
185
|
+
var clientToolsSchema = z3.record(
|
|
186
|
+
z3.string().min(1),
|
|
187
|
+
z3.strictObject({
|
|
188
|
+
inputSchema: jsonSchema,
|
|
189
|
+
description: z3.string().min(1)
|
|
190
|
+
})
|
|
191
|
+
).optional();
|
|
192
|
+
var turnBodySchema = z3.strictObject({
|
|
193
|
+
sessionId: sessionIdSchema,
|
|
194
|
+
message: z3.unknown(),
|
|
195
|
+
trigger: z3.enum(["submit-message", "regenerate-message"]),
|
|
196
|
+
tools: clientToolsSchema,
|
|
197
|
+
elements: elementsSchema
|
|
198
|
+
});
|
|
199
|
+
function http(runtime, ...projections) {
|
|
200
|
+
const contributions = projections.map(
|
|
201
|
+
(projection) => projection.project(runtime)
|
|
202
|
+
);
|
|
203
|
+
const capabilities = {
|
|
204
|
+
history: { path: HISTORY_ROUTE_PATH },
|
|
205
|
+
chat: { path: "/session" },
|
|
206
|
+
events: { path: EVENTS_ROUTE_PATH }
|
|
207
|
+
};
|
|
208
|
+
for (const { capabilities: contributed } of contributions) {
|
|
209
|
+
for (const [name, capability] of Object.entries(contributed)) {
|
|
210
|
+
if (!name.trim() || name !== name.trim()) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`http: capability name "${name}" must be non-empty without surrounding whitespace`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
if (!capability.path.startsWith("/")) {
|
|
216
|
+
throw new Error(`http: capability "${name}" must use an absolute path`);
|
|
217
|
+
}
|
|
218
|
+
if (Object.hasOwn(capabilities, name)) {
|
|
219
|
+
throw new Error(`http: duplicate capability "${name}"`);
|
|
220
|
+
}
|
|
221
|
+
capabilities[name] = capability;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const app = new Hono();
|
|
225
|
+
app.onError((error, context) => {
|
|
226
|
+
if (error instanceof HTTPException3) {
|
|
227
|
+
return context.json(
|
|
228
|
+
{ cause: error.cause, error: error.message },
|
|
229
|
+
error.status,
|
|
230
|
+
NO_STORE
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
throw error;
|
|
234
|
+
});
|
|
235
|
+
app.on(
|
|
236
|
+
["GET", "HEAD"],
|
|
237
|
+
HEALTH_ROUTE_PATH,
|
|
238
|
+
(context) => context.req.method === "HEAD" ? context.body(null, 200, NO_STORE) : context.json({ ok: true }, 200, NO_STORE)
|
|
239
|
+
);
|
|
240
|
+
app.all(
|
|
241
|
+
HEALTH_ROUTE_PATH,
|
|
242
|
+
(context) => methodNotAllowed(context, "GET, HEAD")
|
|
243
|
+
);
|
|
244
|
+
for (const { publicRoutes } of contributions) {
|
|
245
|
+
if (publicRoutes) app.route("/", publicRoutes);
|
|
246
|
+
}
|
|
247
|
+
app.use("*", async (context, next) => {
|
|
248
|
+
if (!context.get("userId")?.trim()) {
|
|
249
|
+
throw new HTTPException3(401, {
|
|
250
|
+
message: "Authentication required",
|
|
251
|
+
cause: {
|
|
252
|
+
code: "api/unauthenticated",
|
|
253
|
+
detail: "Authentication required to access Zukhruf"
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
await next();
|
|
258
|
+
});
|
|
259
|
+
app.get(INFO_ROUTE_PATH, (context) => {
|
|
260
|
+
const mount = mountPath(context);
|
|
261
|
+
return context.json(
|
|
262
|
+
{
|
|
263
|
+
...runtime.info,
|
|
264
|
+
capabilities: Object.fromEntries(
|
|
265
|
+
Object.entries(capabilities).map(([name, { path, ...detail }]) => [
|
|
266
|
+
name,
|
|
267
|
+
{ href: `${mount}${path}`, ...detail }
|
|
268
|
+
])
|
|
269
|
+
)
|
|
270
|
+
},
|
|
271
|
+
200,
|
|
272
|
+
NO_STORE
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
app.all(INFO_ROUTE_PATH, (context) => methodNotAllowed(context, "GET"));
|
|
276
|
+
app.get(
|
|
277
|
+
HISTORY_ROUTE_PATH,
|
|
278
|
+
async (context) => context.json(
|
|
279
|
+
await runtime.listHistory(context.get("userId")),
|
|
280
|
+
200,
|
|
281
|
+
NO_STORE
|
|
282
|
+
)
|
|
283
|
+
);
|
|
284
|
+
app.all(HISTORY_ROUTE_PATH, (context) => methodNotAllowed(context, "GET"));
|
|
285
|
+
app.get(
|
|
286
|
+
SESSION_ROUTE_PATH,
|
|
287
|
+
validate((payload) => ({
|
|
288
|
+
sessionId: {
|
|
289
|
+
select: payload.params.sessionId,
|
|
290
|
+
against: sessionIdSchema
|
|
291
|
+
}
|
|
292
|
+
})),
|
|
293
|
+
async (context) => {
|
|
294
|
+
const { sessionId } = context.var.input;
|
|
295
|
+
const conversation = {
|
|
296
|
+
chatId: sessionId,
|
|
297
|
+
userId: context.get("userId")
|
|
298
|
+
};
|
|
299
|
+
await requireSession(runtime, conversation);
|
|
300
|
+
return context.json(
|
|
301
|
+
{
|
|
302
|
+
sessionId,
|
|
303
|
+
messages: await runtime.observe(conversation).engine.getMessages()
|
|
304
|
+
},
|
|
305
|
+
200,
|
|
306
|
+
NO_STORE
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
app.post(
|
|
311
|
+
SESSION_ROUTE_PATH,
|
|
312
|
+
validate("application/json", (payload) => ({
|
|
313
|
+
body: {
|
|
314
|
+
select: payload.body,
|
|
315
|
+
against: turnBodySchema
|
|
316
|
+
},
|
|
317
|
+
sessionId: {
|
|
318
|
+
select: payload.params.sessionId,
|
|
319
|
+
against: sessionIdSchema
|
|
320
|
+
}
|
|
321
|
+
})),
|
|
322
|
+
async (context) => {
|
|
323
|
+
const { body, sessionId } = context.var.input;
|
|
324
|
+
if (body.sessionId !== sessionId) {
|
|
325
|
+
throw invalidSessionId("Route and body session IDs must match");
|
|
326
|
+
}
|
|
327
|
+
const conversation = {
|
|
328
|
+
chatId: sessionId,
|
|
329
|
+
userId: context.get("userId")
|
|
330
|
+
};
|
|
331
|
+
const request = await validateTurnRequest(body);
|
|
332
|
+
if (!await runtime.sessionExists(conversation)) {
|
|
333
|
+
if (request.message.role !== "user") {
|
|
334
|
+
throw invalidMessage("A new session must start with a user message");
|
|
335
|
+
}
|
|
336
|
+
if (request.trigger !== "submit-message") {
|
|
337
|
+
throw invalidMessage("A new session cannot regenerate a response");
|
|
338
|
+
}
|
|
339
|
+
await runtime.createSession(conversation);
|
|
340
|
+
}
|
|
341
|
+
const turn = await runtime.enqueue(conversation, request);
|
|
342
|
+
return accepted(context, sessionId, turn.id);
|
|
343
|
+
}
|
|
344
|
+
);
|
|
345
|
+
app.all(
|
|
346
|
+
SESSION_ROUTE_PATH,
|
|
347
|
+
(context) => methodNotAllowed(context, "GET, POST")
|
|
348
|
+
);
|
|
349
|
+
app.get(
|
|
350
|
+
SESSION_TURN_ROUTE_PATH,
|
|
351
|
+
validate((payload) => ({
|
|
352
|
+
sessionId: {
|
|
353
|
+
select: payload.params.sessionId,
|
|
354
|
+
against: sessionIdSchema
|
|
355
|
+
},
|
|
356
|
+
turnId: {
|
|
357
|
+
select: payload.params.turnId,
|
|
358
|
+
against: sessionIdSchema
|
|
359
|
+
}
|
|
360
|
+
})),
|
|
361
|
+
async (context) => {
|
|
362
|
+
const { sessionId, turnId } = context.var.input;
|
|
363
|
+
const conversation = {
|
|
364
|
+
chatId: sessionId,
|
|
365
|
+
userId: context.get("userId")
|
|
366
|
+
};
|
|
367
|
+
await requireSession(runtime, conversation);
|
|
368
|
+
const status = await runtime.observe(conversation).status(turnId);
|
|
369
|
+
if (!status) {
|
|
370
|
+
throw new HTTPException3(404, {
|
|
371
|
+
message: "Session turn not found",
|
|
372
|
+
cause: {
|
|
373
|
+
code: "zukhruf/session-turn-not-found",
|
|
374
|
+
detail: `Turn ${turnId} does not exist in session ${sessionId}`
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return context.json({ sessionId, turnId, ...status }, 200, NO_STORE);
|
|
379
|
+
}
|
|
380
|
+
);
|
|
381
|
+
app.all(
|
|
382
|
+
SESSION_TURN_ROUTE_PATH,
|
|
383
|
+
(context) => methodNotAllowed(context, "GET")
|
|
384
|
+
);
|
|
385
|
+
app.post(
|
|
386
|
+
SESSION_TURN_CANCEL_ROUTE_PATH,
|
|
387
|
+
validate((payload) => ({
|
|
388
|
+
sessionId: {
|
|
389
|
+
select: payload.params.sessionId,
|
|
390
|
+
against: sessionIdSchema
|
|
391
|
+
},
|
|
392
|
+
turnId: {
|
|
393
|
+
select: payload.params.turnId,
|
|
394
|
+
against: sessionIdSchema
|
|
395
|
+
}
|
|
396
|
+
})),
|
|
397
|
+
async (context) => {
|
|
398
|
+
const { sessionId, turnId } = context.var.input;
|
|
399
|
+
const conversation = {
|
|
400
|
+
chatId: sessionId,
|
|
401
|
+
userId: context.get("userId")
|
|
402
|
+
};
|
|
403
|
+
await requireSession(runtime, conversation);
|
|
404
|
+
await runtime.observe(conversation).cancel(turnId);
|
|
405
|
+
return context.body(null, 204, NO_STORE);
|
|
406
|
+
}
|
|
407
|
+
);
|
|
408
|
+
app.all(
|
|
409
|
+
SESSION_TURN_CANCEL_ROUTE_PATH,
|
|
410
|
+
(context) => methodNotAllowed(context, "POST")
|
|
411
|
+
);
|
|
412
|
+
app.post(
|
|
413
|
+
SESSION_CANCEL_ROUTE_PATH,
|
|
414
|
+
validate((payload) => ({
|
|
415
|
+
sessionId: {
|
|
416
|
+
select: payload.params.sessionId,
|
|
417
|
+
against: sessionIdSchema
|
|
418
|
+
}
|
|
419
|
+
})),
|
|
420
|
+
async (context) => {
|
|
421
|
+
const { sessionId } = context.var.input;
|
|
422
|
+
const conversation = {
|
|
423
|
+
chatId: sessionId,
|
|
424
|
+
userId: context.get("userId")
|
|
425
|
+
};
|
|
426
|
+
await requireSession(runtime, conversation);
|
|
427
|
+
await runtime.observe(conversation).cancel();
|
|
428
|
+
return context.body(null, 204, NO_STORE);
|
|
429
|
+
}
|
|
430
|
+
);
|
|
431
|
+
app.all(
|
|
432
|
+
SESSION_CANCEL_ROUTE_PATH,
|
|
433
|
+
(context) => methodNotAllowed(context, "POST")
|
|
434
|
+
);
|
|
435
|
+
const eventSources = [
|
|
436
|
+
async (userId, signal) => {
|
|
437
|
+
const events = await runtime.subscribeConversationStatus(signal);
|
|
438
|
+
return {
|
|
439
|
+
async *[Symbol.asyncIterator]() {
|
|
440
|
+
for await (const event of events) {
|
|
441
|
+
if (event.type === "reset") {
|
|
442
|
+
yield event;
|
|
443
|
+
} else if (event.conversation.userId === userId) {
|
|
444
|
+
yield {
|
|
445
|
+
type: "change",
|
|
446
|
+
resource: "conversation",
|
|
447
|
+
id: event.conversation.chatId,
|
|
448
|
+
status: event.status,
|
|
449
|
+
...event.child ? { child: event.child } : {}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
},
|
|
456
|
+
...contributions.flatMap(({ events }) => events ? [events] : [])
|
|
457
|
+
];
|
|
458
|
+
app.get(EVENTS_ROUTE_PATH, (context) => {
|
|
459
|
+
const userId = context.get("userId");
|
|
460
|
+
const abort = new AbortController();
|
|
461
|
+
const requestSignal = context.req.raw.signal;
|
|
462
|
+
const abortRequest = () => abort.abort();
|
|
463
|
+
requestSignal.addEventListener("abort", abortRequest, { once: true });
|
|
464
|
+
context.header("cache-control", "no-store");
|
|
465
|
+
return streamSSE(context, async (stream) => {
|
|
466
|
+
stream.onAbort(abortRequest);
|
|
467
|
+
let lastWrite = Promise.resolve();
|
|
468
|
+
const write = (event) => {
|
|
469
|
+
const next = lastWrite.then(
|
|
470
|
+
() => stream.writeSSE({ data: JSON.stringify(event) })
|
|
471
|
+
);
|
|
472
|
+
lastWrite = next.catch(() => {
|
|
473
|
+
});
|
|
474
|
+
return next;
|
|
475
|
+
};
|
|
476
|
+
const heartbeat = setInterval(() => {
|
|
477
|
+
const next = lastWrite.then(async () => {
|
|
478
|
+
await stream.write(": heartbeat\n\n");
|
|
479
|
+
});
|
|
480
|
+
lastWrite = next.catch(() => {
|
|
481
|
+
});
|
|
482
|
+
}, 15e3);
|
|
483
|
+
try {
|
|
484
|
+
const subscriptions = await Promise.all(
|
|
485
|
+
eventSources.map((subscribe) => subscribe(userId, abort.signal))
|
|
486
|
+
);
|
|
487
|
+
await write({ type: "ready" });
|
|
488
|
+
await Promise.all(
|
|
489
|
+
subscriptions.map(async (events) => {
|
|
490
|
+
for await (const event of events) {
|
|
491
|
+
await write(event.type === "reset" ? { type: "ready" } : event);
|
|
492
|
+
}
|
|
493
|
+
})
|
|
494
|
+
);
|
|
495
|
+
} catch (error) {
|
|
496
|
+
if (!(error instanceof Error && error.name === "AbortError")) {
|
|
497
|
+
throw error;
|
|
498
|
+
}
|
|
499
|
+
} finally {
|
|
500
|
+
clearInterval(heartbeat);
|
|
501
|
+
abort.abort();
|
|
502
|
+
requestSignal.removeEventListener("abort", abortRequest);
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
app.all(EVENTS_ROUTE_PATH, (context) => methodNotAllowed(context, "GET"));
|
|
507
|
+
app.get(
|
|
508
|
+
SESSION_STREAM_ROUTE_PATH,
|
|
509
|
+
validate((payload) => ({
|
|
510
|
+
sessionId: {
|
|
511
|
+
select: payload.params.sessionId,
|
|
512
|
+
against: sessionIdSchema
|
|
513
|
+
}
|
|
514
|
+
})),
|
|
515
|
+
async (context) => {
|
|
516
|
+
const { sessionId } = context.var.input;
|
|
517
|
+
const conversation = {
|
|
518
|
+
chatId: sessionId,
|
|
519
|
+
userId: context.get("userId")
|
|
520
|
+
};
|
|
521
|
+
await requireSession(runtime, conversation);
|
|
522
|
+
const stream = await runtime.observe(conversation).resume();
|
|
523
|
+
if (!stream) {
|
|
524
|
+
throw new HTTPException3(404, {
|
|
525
|
+
message: "Session stream not found",
|
|
526
|
+
cause: {
|
|
527
|
+
code: "zukhruf/session-stream-not-found",
|
|
528
|
+
detail: `No durable stream exists for session ${sessionId}`
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
return createUIMessageStreamResponse({
|
|
533
|
+
headers: NO_STORE,
|
|
534
|
+
stream
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
);
|
|
538
|
+
app.all(
|
|
539
|
+
SESSION_STREAM_ROUTE_PATH,
|
|
540
|
+
(context) => methodNotAllowed(context, "GET")
|
|
541
|
+
);
|
|
542
|
+
for (const { authenticatedRoutes } of contributions) {
|
|
543
|
+
if (authenticatedRoutes) app.route("/", authenticatedRoutes);
|
|
544
|
+
}
|
|
545
|
+
return app;
|
|
546
|
+
}
|
|
547
|
+
function accepted(context, sessionId, turnId) {
|
|
548
|
+
return context.json({ ok: true, sessionId, turnId }, 202, {
|
|
549
|
+
...NO_STORE,
|
|
550
|
+
[ZUKHRUF_SESSION_ID_HEADER]: sessionId,
|
|
551
|
+
[ZUKHRUF_TURN_ID_HEADER]: turnId
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
async function validateTurnMessage(value) {
|
|
555
|
+
const result = await safeValidateUIMessages({ messages: [value] });
|
|
556
|
+
if (!result.success) throw invalidMessage(result.error.message);
|
|
557
|
+
const message = result.data[0];
|
|
558
|
+
if (!isTurnMessage(message))
|
|
559
|
+
throw invalidMessage("System messages are not accepted");
|
|
560
|
+
return message;
|
|
561
|
+
}
|
|
562
|
+
async function validateTurnRequest(body) {
|
|
563
|
+
const message = await validateTurnMessage(body.message);
|
|
564
|
+
const tools = body.tools === void 0 ? {} : { tools: body.tools };
|
|
565
|
+
const elements = body.elements === void 0 ? {} : { elements: body.elements };
|
|
566
|
+
if (message.role === "assistant") {
|
|
567
|
+
if (body.trigger === "regenerate-message") {
|
|
568
|
+
throw invalidMessage(
|
|
569
|
+
"Regeneration requires the user message preceding the assistant response"
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
return { message, trigger: "submit-message", ...tools, ...elements };
|
|
573
|
+
}
|
|
574
|
+
return { message, trigger: body.trigger, ...tools, ...elements };
|
|
575
|
+
}
|
|
576
|
+
function isTurnMessage(message) {
|
|
577
|
+
return message.role === "user" || message.role === "assistant";
|
|
578
|
+
}
|
|
579
|
+
function invalidMessage(detail) {
|
|
580
|
+
return new HTTPException3(400, {
|
|
581
|
+
message: "Invalid message",
|
|
582
|
+
cause: { code: "api/validation-failed", detail }
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
function invalidSessionId(detail) {
|
|
586
|
+
return new HTTPException3(400, {
|
|
587
|
+
message: "Invalid session ID",
|
|
588
|
+
cause: { code: "api/validation-failed", detail }
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
async function requireSession(runtime, conversation) {
|
|
592
|
+
if (await runtime.sessionExists(conversation)) return;
|
|
593
|
+
throw new HTTPException3(404, {
|
|
594
|
+
message: "Session not found",
|
|
595
|
+
cause: {
|
|
596
|
+
code: "zukhruf/session-not-found",
|
|
597
|
+
detail: `Session ${conversation.chatId} does not exist`
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
function methodNotAllowed(context, allow) {
|
|
602
|
+
context.header("Allow", allow);
|
|
603
|
+
throw new HTTPException3(405, {
|
|
604
|
+
message: "Method not allowed",
|
|
605
|
+
cause: {
|
|
606
|
+
code: "api/method-not-allowed",
|
|
607
|
+
detail: `This endpoint only accepts ${allow} requests`
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
export {
|
|
612
|
+
ZUKHRUF_SESSION_ID_HEADER,
|
|
613
|
+
ZUKHRUF_TURN_ID_HEADER,
|
|
614
|
+
http,
|
|
615
|
+
mountPath,
|
|
616
|
+
projectHttp
|
|
617
|
+
};
|
|
618
|
+
//# sourceMappingURL=index.js.map
|