@restatedev/agents 0.0.0-stage → 0.1.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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +689 -2
  3. package/dist/calls.d.ts +29 -0
  4. package/dist/calls.d.ts.map +1 -0
  5. package/dist/calls.js +91 -0
  6. package/dist/calls.js.map +1 -0
  7. package/dist/claude/conversation.d.ts +33 -0
  8. package/dist/claude/conversation.d.ts.map +1 -0
  9. package/dist/claude/conversation.js +95 -0
  10. package/dist/claude/conversation.js.map +1 -0
  11. package/dist/claude/index.d.ts +78 -0
  12. package/dist/claude/index.d.ts.map +1 -0
  13. package/dist/claude/index.js +269 -0
  14. package/dist/claude/index.js.map +1 -0
  15. package/dist/client.d.ts +83 -0
  16. package/dist/client.d.ts.map +1 -0
  17. package/dist/client.js +88 -0
  18. package/dist/client.js.map +1 -0
  19. package/dist/common.d.ts +284 -0
  20. package/dist/common.d.ts.map +1 -0
  21. package/dist/common.js +475 -0
  22. package/dist/common.js.map +1 -0
  23. package/dist/deepseek/index.d.ts +69 -0
  24. package/dist/deepseek/index.d.ts.map +1 -0
  25. package/dist/deepseek/index.js +381 -0
  26. package/dist/deepseek/index.js.map +1 -0
  27. package/dist/deepseek/persistence.d.ts +61 -0
  28. package/dist/deepseek/persistence.d.ts.map +1 -0
  29. package/dist/deepseek/persistence.js +224 -0
  30. package/dist/deepseek/persistence.js.map +1 -0
  31. package/dist/index.d.ts +15 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +37 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/langgraph/checkpointer.d.ts +29 -0
  36. package/dist/langgraph/checkpointer.d.ts.map +1 -0
  37. package/dist/langgraph/checkpointer.js +275 -0
  38. package/dist/langgraph/checkpointer.js.map +1 -0
  39. package/dist/langgraph/index.d.ts +67 -0
  40. package/dist/langgraph/index.d.ts.map +1 -0
  41. package/dist/langgraph/index.js +265 -0
  42. package/dist/langgraph/index.js.map +1 -0
  43. package/dist/log.d.ts +29 -0
  44. package/dist/log.d.ts.map +1 -0
  45. package/dist/log.js +145 -0
  46. package/dist/log.js.map +1 -0
  47. package/dist/object.d.ts +181 -0
  48. package/dist/object.d.ts.map +1 -0
  49. package/dist/object.js +232 -0
  50. package/dist/object.js.map +1 -0
  51. package/dist/opencode/db.d.ts +40 -0
  52. package/dist/opencode/db.d.ts.map +1 -0
  53. package/dist/opencode/db.js +161 -0
  54. package/dist/opencode/db.js.map +1 -0
  55. package/dist/opencode/index.d.ts +101 -0
  56. package/dist/opencode/index.d.ts.map +1 -0
  57. package/dist/opencode/index.js +464 -0
  58. package/dist/opencode/index.js.map +1 -0
  59. package/dist/output.d.ts +17 -0
  60. package/dist/output.d.ts.map +1 -0
  61. package/dist/output.js +81 -0
  62. package/dist/output.js.map +1 -0
  63. package/dist/pi/index.d.ts +90 -0
  64. package/dist/pi/index.d.ts.map +1 -0
  65. package/dist/pi/index.js +313 -0
  66. package/dist/pi/index.js.map +1 -0
  67. package/dist/pi/storage.d.ts +5 -0
  68. package/dist/pi/storage.d.ts.map +1 -0
  69. package/dist/pi/storage.js +41 -0
  70. package/dist/pi/storage.js.map +1 -0
  71. package/dist/signals.d.ts +195 -0
  72. package/dist/signals.d.ts.map +1 -0
  73. package/dist/signals.js +258 -0
  74. package/dist/signals.js.map +1 -0
  75. package/dist/tools.d.ts +158 -0
  76. package/dist/tools.d.ts.map +1 -0
  77. package/dist/tools.js +211 -0
  78. package/dist/tools.js.map +1 -0
  79. package/dist/webhook.d.ts +73 -0
  80. package/dist/webhook.d.ts.map +1 -0
  81. package/dist/webhook.js +104 -0
  82. package/dist/webhook.js.map +1 -0
  83. package/package.json +224 -3
  84. package/src/calls.ts +138 -0
  85. package/src/claude/README.md +84 -0
  86. package/src/claude/conversation.ts +114 -0
  87. package/src/claude/index.ts +364 -0
  88. package/src/client.ts +179 -0
  89. package/src/common.ts +593 -0
  90. package/src/deepseek/README.md +110 -0
  91. package/src/deepseek/index.ts +487 -0
  92. package/src/deepseek/persistence.ts +293 -0
  93. package/src/index.ts +60 -0
  94. package/src/langgraph/README.md +87 -0
  95. package/src/langgraph/checkpointer.ts +364 -0
  96. package/src/langgraph/index.ts +349 -0
  97. package/src/log.ts +193 -0
  98. package/src/object.ts +343 -0
  99. package/src/opencode/README.md +125 -0
  100. package/src/opencode/db.ts +223 -0
  101. package/src/opencode/index.ts +595 -0
  102. package/src/output.ts +95 -0
  103. package/src/pi/README.md +105 -0
  104. package/src/pi/index.ts +402 -0
  105. package/src/pi/storage.ts +65 -0
  106. package/src/signals.ts +426 -0
  107. package/src/tools.ts +343 -0
  108. package/src/webhook.ts +163 -0
package/dist/tools.js ADDED
@@ -0,0 +1,211 @@
1
+ // Tools for any of the frameworks: defined once with `tool()`, given to any
2
+ // agent's `tools`, which turns them into the framework's own. And the tools
3
+ // that are Restate itself: calling a handler, sending to one, running a
4
+ // workflow, and waiting for a signal.
5
+ //
6
+ // A tool runs with the agent's context, and with the id of the model's tool
7
+ // call: Restate calls carry an idempotency key made of the invocation id and
8
+ // that id, so a tool call run again after a crash makes the same call, once.
9
+ import * as restate from '@restatedev/sessions';
10
+ import { z } from 'zod';
11
+ import { stopSignalOf, configured, ToolError } from './common.js';
12
+ import { callOf } from './calls.js';
13
+ import { signals, signalsOf } from './signals.js';
14
+ const TOOL = Symbol.for('restate-agents.tool');
15
+ /** The JSON schema of `t`'s input, which a framework offers the model. */
16
+ export const jsonSchemaOf = (t) => z.toJSONSchema(t.input);
17
+ /** Whether `value` is a {@link Tool}, rather than a framework's own. */
18
+ export const isTool = (value) => typeof value === 'object' && value !== null && TOOL in value;
19
+ /** A result as text: a string as it is, nothing as "done", anything else as JSON. */
20
+ const text = (value) => typeof value === 'string' ? value : value === null || value === undefined ? 'done' : JSON.stringify(value);
21
+ /**
22
+ * A tool, for any framework's agent:
23
+ *
24
+ * const weather = tool({
25
+ * name: 'get_weather',
26
+ * description: 'The current weather in a city.',
27
+ * input: z.object({ city: z.string() }),
28
+ * run: async ({ city }) => `Sunny in ${city}`,
29
+ * });
30
+ *
31
+ * `run` returns the result (or a promise of it), text or anything JSON; it
32
+ * gets the agent's `ctx`, the tool call's `callId`, and an `idempotencyKey`
33
+ * made of both, for what the tool must do once. A `restate.TerminalError` it throws (or
34
+ * a Restate call it makes fails with) is its answer, `Error: <message>`, for
35
+ * the model to act on; and so are arguments that do not fit `input`, which
36
+ * `run` never sees.
37
+ */
38
+ export const tool = (options) => ({
39
+ [TOOL]: true,
40
+ name: options.name,
41
+ description: options.description,
42
+ input: options.input,
43
+ run: async (args, { ctx, callId }) => {
44
+ // Arguments that do not fit: the model's to fix. Failing the attempt
45
+ // instead, a retry would meet the same stored call again, and fail again.
46
+ const parsed = options.input.safeParse(args);
47
+ if (!parsed.success)
48
+ return `Error: the arguments do not fit the tool's input: ${z.prettifyError(parsed.error)}`;
49
+ const input = parsed.data;
50
+ try {
51
+ return text(await options.run(input, { ctx, callId, idempotencyKey: `${ctx.request.id}:${callId}` }));
52
+ }
53
+ catch (error) {
54
+ // The turn stopped (its invocation was cancelled, or its attempt
55
+ // ended): it ends, rather than the model going on with the tool's error
56
+ // as its answer (such as the failure of a call that the cancellation
57
+ // cancelled).
58
+ const turn = stopSignalOf(ctx);
59
+ if (turn?.aborted)
60
+ throw turn.reason;
61
+ if (error instanceof restate.CancelledError)
62
+ throw error;
63
+ // A terminal error is the tool's answer, which the model can act on; any
64
+ // other error fails the attempt, which Restate retries.
65
+ if (error instanceof restate.TerminalError)
66
+ return `Error: ${error.message}`;
67
+ throw new ToolError(options.name, error);
68
+ }
69
+ },
70
+ });
71
+ /**
72
+ * `body`, a Restate call of tool `name` with `input`, made once however often
73
+ * the tool call runs (see calls.ts): it gets the id of the call, which an
74
+ * earlier attempt may have made, and so its idempotency key.
75
+ */
76
+ async function once(name, input, context, body) {
77
+ const { ctx } = context;
78
+ const call = await callOf(ctx, name, input, context.callId);
79
+ try {
80
+ const result = await body({ ctx, callId: call.id, idempotencyKey: `${ctx.request.id}:${call.id}` });
81
+ await call.answered();
82
+ return result;
83
+ }
84
+ catch (error) {
85
+ // A terminal error answers the call; any other fails the attempt, and the
86
+ // retry makes the call again.
87
+ if (error instanceof restate.TerminalError)
88
+ await call.answered(`Error: ${error.message}`);
89
+ throw error;
90
+ }
91
+ }
92
+ /** Whether a target is keyed: a virtual object (key) or a workflow (id). */
93
+ const keyed = (target) => 'object' in target || 'workflow' in target;
94
+ /** Call `method` of `service`, with JSON in and out, once per tool call (see {@link ToolContext}). */
95
+ const invoke = ({ ctx, idempotencyKey }, target) => ctx.rpc({
96
+ service: target.service,
97
+ handler: target.method,
98
+ key: target.key,
99
+ input: target.parameter,
100
+ idempotencyKey,
101
+ });
102
+ /** The tool's input: the handler's, and the key if the model chooses it. */
103
+ function inputOf(target, { input, key }) {
104
+ const shape = input?.shape ?? {};
105
+ if (!keyed(target) || key !== undefined)
106
+ return z.object(shape);
107
+ return z.object({ key: z.string().describe(`Which ${target.name}: its key`), ...shape });
108
+ }
109
+ /** The key, and the handler's input, of a tool call's input. */
110
+ async function split(target, options, all, ctx) {
111
+ if (!keyed(target))
112
+ return { key: undefined, parameter: options.input ? all : undefined };
113
+ if (options.key !== undefined) {
114
+ return { key: await configured(options.key, ctx), parameter: options.input ? all : undefined };
115
+ }
116
+ const { key, ...rest } = all;
117
+ return { key, parameter: options.input ? rest : undefined };
118
+ }
119
+ /**
120
+ * A tool that calls `handler` of `target` and answers with its result:
121
+ *
122
+ * call(orders, 'status', {
123
+ * description: 'The status of an order.',
124
+ * input: z.object({ order: z.string() }),
125
+ * })
126
+ *
127
+ * Of a virtual object, the model chooses the key, unless `key` gives it.
128
+ */
129
+ export const call = (target, handler, options) => tool({
130
+ name: options.name ?? `${target.name}_${handler}`,
131
+ description: options.description,
132
+ input: inputOf(target, options),
133
+ run: async (input, context) => once(`${target.name}/${handler}`, input, context, async (c) => invoke(c, { service: target.name, method: handler, ...(await split(target, options, input, c.ctx)) })),
134
+ });
135
+ /**
136
+ * A tool that sends to `handler` of `target`, without waiting for it: a
137
+ * one-way call, made once, after `delay` if given (such as `{ minutes: 5 }`).
138
+ */
139
+ export const send = (target, handler, options) => tool({
140
+ name: options.name ?? `${target.name}_${handler}`,
141
+ description: options.description,
142
+ input: inputOf(target, options),
143
+ run: async (input, context) => once(`${target.name}/${handler}`, input, context, async ({ ctx, idempotencyKey }) => {
144
+ const { key, parameter } = await split(target, options, input, ctx);
145
+ ctx.send({ service: target.name, handler, key, input: parameter, delay: options.delay, idempotencyKey });
146
+ return `sent to ${target.name}${key === undefined ? '' : `/${key}`}/${handler}`;
147
+ }),
148
+ });
149
+ /**
150
+ * A tool that runs the workflow `target` (its `run` handler) to the end, and
151
+ * answers with its result. Each tool call is a workflow of its own, with an
152
+ * id made of the agent's key and the tool call's id, unless `key` gives one.
153
+ */
154
+ export const workflow = (target, options) => tool({
155
+ name: options.name ?? target.name,
156
+ description: options.description,
157
+ input: z.object(options.input?.shape ?? {}),
158
+ run: async (input, context) => once(target.name, input, context, async (c) => invoke(c, {
159
+ service: target.name,
160
+ method: 'run',
161
+ key: options.key === undefined ? `${c.ctx.key}-${c.callId}` : await configured(options.key, c.ctx),
162
+ parameter: options.input ? input : undefined,
163
+ })),
164
+ });
165
+ export function subagent(agent, { description = agent.description, name: toolName = agent.name, handler, } = {}) {
166
+ return tool({
167
+ name: toolName,
168
+ description: `Hand a task to the ${agent.name} agent${description ? ` (${description})` : ''}, and get its answer. ` +
169
+ 'Tasks of different names run at once; the same name continues the same task.',
170
+ input: z.object({
171
+ name: z
172
+ .string()
173
+ .regex(/^[a-z0-9-]+$/)
174
+ .describe('A short name for the task, such as "fizzbuzz"'),
175
+ task: z.string().describe('The task in full: the agent sees nothing else'),
176
+ }),
177
+ run: async ({ name, task }, context) => once(agent.name, { name, task }, context, (c) => invoke(c, { service: agent.name, key: `${c.ctx.key}.${name}`, method: handler ?? 'chat', parameter: task })),
178
+ });
179
+ }
180
+ /**
181
+ * A tool that waits for the signal `name` to the agent, and answers with its
182
+ * value: a person's approval, say, or another system's result. The agent
183
+ * waits without holding a process. With an `input`, the model says what it
184
+ * waits for (such as the refund to approve), which `pending` shows.
185
+ *
186
+ * signal('approval', {
187
+ * description: 'Wait for a manager to approve a refund.',
188
+ * input: z.object({ order: z.string(), amount: z.number() }),
189
+ * })
190
+ *
191
+ * Each wait has an id: `pending` (each `sessionObject()` has it) lists them, and
192
+ * the agent's `signal` handler resolves one, by its id, or the oldest wait
193
+ * for a name:
194
+ *
195
+ * curl localhost:8080/support/alice/pending
196
+ * curl localhost:8080/support/alice/signal --json '{"id": "inv_1f…:toolu_01", "value": "approved"}'
197
+ * curl localhost:8080/support/alice/signal --json '{"name": "approval", "value": "approved"}'
198
+ *
199
+ * It answers with the id of the wait it resolved, or `null` if none waited:
200
+ * the signal is dropped then, unless it has `"keep": true`, which keeps it
201
+ * for the next wait for its name.
202
+ */
203
+ export const signal = (name, options) => tool({
204
+ name: options.name ?? `wait_for_${name}`,
205
+ description: options.description,
206
+ input: z.object(options.input?.shape ?? {}),
207
+ run: async (input, context) => once(`signal/${name}`, input, context, async ({ ctx, idempotencyKey: id }) => ctx
208
+ .rpc(signals, signalsOf(ctx), { idempotencyKey: id })
209
+ .wait({ id, name, ...(options.input ? { input } : {}) })),
210
+ });
211
+ //# sourceMappingURL=tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,4EAA4E;AAC5E,wEAAwE;AACxE,sCAAsC;AACtC,EAAE;AACF,4EAA4E;AAC5E,6EAA6E;AAC7E,6EAA6E;AAC7E,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAkB,UAAU,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAA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;SACpD,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAC3D;CACJ,CAAC,CAAC"}
@@ -0,0 +1,73 @@
1
+ import { sdk as restate } from '@restatedev/sessions/internal';
2
+ /** How a webhook is signed: the HMAC-SHA256 of its body with `secret`, in hex, in the request's `header`. */
3
+ export interface WebhookAuth {
4
+ /** The header with the signature, such as `linear-signature` or `X-Hub-Signature-256` (GitHub's `sha256=` prefix is taken too), in any case. */
5
+ header: string;
6
+ /** The webhook's signing secret, which the other system shows when the webhook is made. */
7
+ secret: string;
8
+ }
9
+ /** An agent (an `sessionObject()` or `agent()` of @restatedev/agents, with a `chat` handler) that webhooks go to. */
10
+ type Route = restate.VirtualObjectDefinition<string, {
11
+ chat: unknown;
12
+ }>;
13
+ /**
14
+ * A delivery's body: JSON, as the other system sends it, read as it is (it is
15
+ * theirs to shape, not checked here).
16
+ */
17
+ export type WebhookEvent = Record<string, any>;
18
+ /** A webhook service's options. */
19
+ export interface WebhookOptions<P extends string = string> {
20
+ /** The service's name, which its URL starts with: `/<name>/webhook`. */
21
+ name: P;
22
+ /** What the webhook is, for the UI and the admin API. */
23
+ description?: string;
24
+ /** Its signature: a webhook without it is refused (401). Without `auth`, any request is taken. */
25
+ auth?: WebhookAuth;
26
+ /**
27
+ * The header with the delivery's id, which a delivery sent again has too
28
+ * (such as `linear-delivery`): it reaches the same message, not a second
29
+ * one. Without it, each request is a delivery of its own. In any case, as
30
+ * header names are.
31
+ */
32
+ idempotencyHeader?: string;
33
+ /** The agents each delivery goes to. */
34
+ route: Route[];
35
+ /**
36
+ * The key of the agents' object that a delivery goes to, from its body
37
+ * (the issue, say: one conversation per issue), or undefined to drop the
38
+ * delivery. By default, the delivery's id: a conversation per delivery.
39
+ * The event is the body's JSON, untyped: `key: ({ data }) => data.identifier`.
40
+ */
41
+ key?(this: void, event: WebhookEvent): string | undefined;
42
+ /**
43
+ * What of a delivery the agents read, from its body: by default, all of
44
+ * it. Some systems send much more than an agent needs (GitHub sends the
45
+ * whole repository with each event): `message: ({ comment }) => ({ body: comment.body })`.
46
+ */
47
+ message?(this: void, event: WebhookEvent): unknown;
48
+ }
49
+ /**
50
+ * The message an agent gets for a webhook's delivery: its body, as JSON, in
51
+ * a `<webhook>` element that says which webhook, and which delivery.
52
+ *
53
+ * <webhook name="linear" delivery="5d2…">
54
+ * {"action": "create", "type": "Issue", ...}
55
+ * </webhook>
56
+ */
57
+ export declare const webhookMessage: (name: string, delivery: string, event: unknown) => string;
58
+ /**
59
+ * A webhook into agents: a service `name`, whose `webhook` handler checks a
60
+ * delivery's signature, and delivers it to each agent of `route`, keyed by
61
+ * `key`, as a {@link webhookMessage}, through the agent's `signals`: to the
62
+ * message the agent answers, if any, which takes it at its next step (as
63
+ * `steer`), or else as a message of its own (as `chat`). A turn that ends,
64
+ * or is stopped, before it takes it answers it after. It answers once the
65
+ * deliveries are sent, not answered: a system that waits seconds for an
66
+ * answer (Linear, GitHub) gets one at once. What it is (its routes, auth and
67
+ * idempotency) is in the handler's metadata, which the admin API shows.
68
+ */
69
+ export declare const webhook: <P extends string>({ name, description, auth, idempotencyHeader, route, key, message, }: WebhookOptions<P>) => restate.ServiceDefinition<P, {
70
+ webhook: (ctx: restate.Context, input: Uint8Array<ArrayBufferLike>) => Promise<void>;
71
+ }>;
72
+ export {};
73
+ //# sourceMappingURL=webhook.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhook.d.ts","sourceRoot":"","sources":["../src/webhook.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAG/D,6GAA6G;AAC7G,MAAM,WAAW,WAAW;IAC1B,gJAAgJ;IAChJ,MAAM,EAAE,MAAM,CAAC;IACf,2FAA2F;IAC3F,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qHAAqH;AACrH,KAAK,KAAK,GAAG,OAAO,CAAC,uBAAuB,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AAExE;;;GAGG;AAEH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE/C,mCAAmC;AACnC,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IACvD,wEAAwE;IACxE,IAAI,EAAE,CAAC,CAAC;IACR,yDAAyD;IACzD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kGAAkG;IAClG,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,wCAAwC;IACxC,KAAK,EAAE,KAAK,EAAE,CAAC;IACf;;;;;OAKG;IACH,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,GAAG,MAAM,GAAG,SAAS,CAAC;IAC1D;;;;OAIG;IACH,OAAO,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC;CACpD;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,SAAU,MAAM,YAAY,MAAM,SAAS,OAAO,WACqB,CAAC;AAuBnG;;;;;;;;;;GAUG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,MAAM,wEAQrC,cAAc,CAAC,CAAC,CAAC;;EAyCnB,CAAC"}
@@ -0,0 +1,104 @@
1
+ // Webhooks into agents: a Restate service whose one handler, `webhook`,
2
+ // takes another system's webhook (Linear's, GitHub's...), checks its
3
+ // signature, and delivers it to agents, as a message saying where it comes
4
+ // from: to the message an agent answers, if it answers one, or else as a
5
+ // message of its own.
6
+ //
7
+ // const issues = webhook({
8
+ // name: 'linear',
9
+ // auth: { header: 'linear-signature', secret: process.env.LINEAR_WEBHOOK_SECRET! },
10
+ // idempotencyHeader: 'linear-delivery',
11
+ // route: [triage],
12
+ // key: ({ data }) => data.identifier,
13
+ // });
14
+ // await serve(issues, triage);
15
+ //
16
+ // The other system posts to Restate's ingress, at `/<name>/webhook`.
17
+ import { createHmac, timingSafeEqual } from 'node:crypto';
18
+ import { sdk as restate } from '@restatedev/sessions/internal';
19
+ import { deliver, hasCompanion } from './signals.js';
20
+ /**
21
+ * The message an agent gets for a webhook's delivery: its body, as JSON, in
22
+ * a `<webhook>` element that says which webhook, and which delivery.
23
+ *
24
+ * <webhook name="linear" delivery="5d2…">
25
+ * {"action": "create", "type": "Issue", ...}
26
+ * </webhook>
27
+ */
28
+ export const webhookMessage = (name, delivery, event) => `<webhook name="${name}" delivery="${delivery}">\n${JSON.stringify(event, null, 2)}\n</webhook>`;
29
+ /** Whether `signature` is the HMAC-SHA256 of `body` with `secret`, in hex. */
30
+ const signed = (body, signature, secret) => {
31
+ const expected = createHmac('sha256', secret).update(body).digest();
32
+ const given = Buffer.from((signature ?? '').replace(/^sha256=/, ''), 'hex');
33
+ return given.length === expected.length && timingSafeEqual(given, expected);
34
+ };
35
+ /** `body` as a JSON object, or a terminal error (400) if it is not. */
36
+ const parsed = (body) => {
37
+ let event;
38
+ try {
39
+ event = JSON.parse(new TextDecoder().decode(body));
40
+ }
41
+ catch {
42
+ throw new restate.TerminalError('The webhook is not JSON', { errorCode: 400 });
43
+ }
44
+ if (typeof event !== 'object' || event === null || Array.isArray(event)) {
45
+ throw new restate.TerminalError('The webhook is not a JSON object', { errorCode: 400 });
46
+ }
47
+ return event;
48
+ };
49
+ /**
50
+ * A webhook into agents: a service `name`, whose `webhook` handler checks a
51
+ * delivery's signature, and delivers it to each agent of `route`, keyed by
52
+ * `key`, as a {@link webhookMessage}, through the agent's `signals`: to the
53
+ * message the agent answers, if any, which takes it at its next step (as
54
+ * `steer`), or else as a message of its own (as `chat`). A turn that ends,
55
+ * or is stopped, before it takes it answers it after. It answers once the
56
+ * deliveries are sent, not answered: a system that waits seconds for an
57
+ * answer (Linear, GitHub) gets one at once. What it is (its routes, auth and
58
+ * idempotency) is in the handler's metadata, which the admin API shows.
59
+ */
60
+ export const webhook = ({ name, description, auth, idempotencyHeader, route, key, message, }) => {
61
+ if (route.length === 0)
62
+ throw new Error(`webhook ${name}: route it to at least one agent`);
63
+ for (const agent of route) {
64
+ if (!hasCompanion(agent))
65
+ throw new Error(`webhook ${name}: ${agent.name} is not a sessionObject() or agent()`);
66
+ }
67
+ if (auth && !auth.secret)
68
+ throw new Error(`webhook ${name}: its auth has no secret (unset in the environment?)`);
69
+ const what = description ?? `A webhook into ${route.map((r) => r.name).join(', ')}`;
70
+ // Restate gives a request's headers by their names in lower case.
71
+ const signatureHeader = auth?.header.toLowerCase();
72
+ const deliveryHeader = idempotencyHeader?.toLowerCase();
73
+ return restate.service({
74
+ name,
75
+ description: what,
76
+ handlers: {
77
+ webhook: restate.handlers.handler({
78
+ input: restate.serde.binary,
79
+ accept: '*/*',
80
+ description: what,
81
+ metadata: {
82
+ 'restate-agents.kind': 'webhook',
83
+ 'restate-agents.routes': route.map((r) => r.name).join(','),
84
+ 'restate-agents.auth': auth ? `hmac-sha256:${auth.header}` : 'none',
85
+ ...(idempotencyHeader ? { 'restate-agents.idempotency-header': idempotencyHeader } : {}),
86
+ },
87
+ }, async (ctx, body) => {
88
+ const headers = ctx.request().headers;
89
+ if (auth && !signed(body, headers.get(signatureHeader), auth.secret)) {
90
+ throw new restate.TerminalError(`Not signed: no valid ${auth.header}`, { errorCode: 401 });
91
+ }
92
+ const event = parsed(body);
93
+ const delivery = (deliveryHeader && headers.get(deliveryHeader)) || ctx.request().id;
94
+ const to = key ? key(event) : delivery;
95
+ if (to === undefined)
96
+ return;
97
+ const text = webhookMessage(name, delivery, message ? message(event) : event);
98
+ for (const agent of route)
99
+ deliver(ctx, agent, to, text, { idempotencyKey: `webhook:${name}:${delivery}` });
100
+ }),
101
+ },
102
+ });
103
+ };
104
+ //# source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G,EAAE,CAAC,CAAC;gBAC7F,CAAC;gBACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;gBAC3B,MAAM,QAAQ,GAAG,CAAC,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;gBACrF,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;gBACvC,IAAI,EAAE,KAAK,SAAS;oBAAE,OAAO;gBAC7B,MAAM,IAAI,GAAG,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;gBAC9E,KAAK,MAAM,KAAK,IAAI,KAAK;oBAAE,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,cAAc,EAAE,WAAW,IAAI,IAAI,QAAQ,EAAE,EAAE,CAAC,CAAC;YAC9G,CAAC,CACF;SACF;KACF,CAAC,CAAC;AACL,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,227 @@
1
1
  {
2
2
  "name": "@restatedev/agents",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Agent frameworks in Restate virtual objects, their state in the object's SQL database: pi, OpenCode, LangGraph, Claude (Anthropic SDK) and DeepSeek Harness",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/restatedev/sdk-sessions.git",
9
+ "directory": "packages/agents"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "src"
14
+ ],
15
+ "type": "module",
16
+ "sideEffects": false,
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./claude": {
23
+ "types": "./dist/claude/index.d.ts",
24
+ "default": "./dist/claude/index.js"
25
+ },
26
+ "./client": {
27
+ "types": "./dist/client.d.ts",
28
+ "default": "./dist/client.js"
29
+ },
30
+ "./deepseek": {
31
+ "types": "./dist/deepseek/index.d.ts",
32
+ "default": "./dist/deepseek/index.js"
33
+ },
34
+ "./langgraph": {
35
+ "types": "./dist/langgraph/index.d.ts",
36
+ "default": "./dist/langgraph/index.js"
37
+ },
38
+ "./opencode": {
39
+ "types": "./dist/opencode/index.d.ts",
40
+ "default": "./dist/opencode/index.js"
41
+ },
42
+ "./opencode/db": {
43
+ "types": "./dist/opencode/db.d.ts",
44
+ "default": "./dist/opencode/db.js"
45
+ },
46
+ "./pi": {
47
+ "types": "./dist/pi/index.d.ts",
48
+ "default": "./dist/pi/index.js"
49
+ },
50
+ "./pi/storage": {
51
+ "types": "./dist/pi/storage.d.ts",
52
+ "default": "./dist/pi/storage.js"
53
+ }
54
+ },
55
+ "dependencies": {
56
+ "@restatedev/restate-sdk-clients": "1.18.2",
57
+ "@restatedev/restate-sdk-core": "1.18.2",
58
+ "@standard-schema/spec": "1.1.0"
59
+ },
60
+ "devDependencies": {
61
+ "@ai-sdk/provider": "3.0.8",
62
+ "@anthropic-ai/sdk": "0.133.0",
63
+ "@deepseek-ai/cordis": "4.0.5",
64
+ "@deepseek-ai/cordis-plugin-loader": "1.0.6",
65
+ "@deepseek-ai/dsh-agent": "0.2.1-rc.2",
66
+ "@deepseek-ai/dsh-agent-loop": "0.2.1-rc.2",
67
+ "@deepseek-ai/dsh-anonymous-user-id": "0.2.1-rc.2",
68
+ "@deepseek-ai/dsh-atomic-write": "0.2.1-rc.2",
69
+ "@deepseek-ai/dsh-attachment": "0.2.1-rc.2",
70
+ "@deepseek-ai/dsh-authorization": "0.2.1-rc.2",
71
+ "@deepseek-ai/dsh-credentials": "0.2.1-rc.2",
72
+ "@deepseek-ai/dsh-deepseek-llm-api-extensions": "0.2.1-rc.2",
73
+ "@deepseek-ai/dsh-fs": "0.2.1-rc.2",
74
+ "@deepseek-ai/dsh-home-paths": "0.2.1-rc.2",
75
+ "@deepseek-ai/dsh-launch-environment": "0.2.1-rc.2",
76
+ "@deepseek-ai/dsh-llm": "0.2.1-rc.2",
77
+ "@deepseek-ai/dsh-llm-deepseek": "0.2.1-rc.2",
78
+ "@deepseek-ai/dsh-llm-deepseek-api-key": "0.2.1-rc.2",
79
+ "@deepseek-ai/dsh-llm-pi-ai": "0.2.1-rc.2",
80
+ "@deepseek-ai/dsh-ptc-runtime": "0.2.1-rc.2",
81
+ "@deepseek-ai/dsh-sandbox": "0.2.1-rc.2",
82
+ "@deepseek-ai/dsh-sandbox-policy": "0.2.1-rc.2",
83
+ "@deepseek-ai/dsh-scope": "0.2.1-rc.2",
84
+ "@deepseek-ai/dsh-session": "0.2.1-rc.2",
85
+ "@deepseek-ai/dsh-session-checkpoint-policy": "0.2.1-rc.2",
86
+ "@deepseek-ai/dsh-session-persistence": "0.2.1-rc.2",
87
+ "@deepseek-ai/dsh-session-projection": "0.2.1-rc.2",
88
+ "@deepseek-ai/dsh-system-prompt": "0.2.1-rc.2",
89
+ "@deepseek-ai/dsh-timeout": "0.2.1-rc.2",
90
+ "@deepseek-ai/dsh-tools": "0.2.1-rc.2",
91
+ "@deepseek-ai/dsh-user-approval": "0.2.1-rc.2",
92
+ "@earendil-works/chord": "1.4.0",
93
+ "@earendil-works/pi-ai": "1.4.0",
94
+ "@earendil-works/pi-durable": "1.4.0",
95
+ "@langchain/anthropic": "1.5.11",
96
+ "@langchain/core": "1.2.12",
97
+ "@langchain/langgraph": "1.4.18",
98
+ "@langchain/langgraph-checkpoint": "1.1.5",
99
+ "@langchain/langgraph-checkpoint-validation": "1.1.1",
100
+ "@langchain/openai": "1.7.0",
101
+ "@opencode/core": "2.0.32",
102
+ "@opencode/plugin": "2.0.32",
103
+ "@opencode/sdk": "2.0.32",
104
+ "@opencode/server": "2.0.32",
105
+ "@restatedev/restate-sdk": "1.18.2",
106
+ "@restatedev/sessions": "0.1.0",
107
+ "effect": "4.0.1",
108
+ "zod": "4.6.5"
109
+ },
110
+ "peerDependencies": {
111
+ "@anthropic-ai/sdk": "^0.133.0",
112
+ "@deepseek-ai/cordis": "~4.0.5",
113
+ "@deepseek-ai/dsh-agent": "0.2.1-rc.2",
114
+ "@deepseek-ai/dsh-agent-loop": "0.2.1-rc.2",
115
+ "@deepseek-ai/dsh-llm": "0.2.1-rc.2",
116
+ "@deepseek-ai/dsh-llm-deepseek-api-key": "0.2.1-rc.2",
117
+ "@deepseek-ai/dsh-llm-pi-ai": "0.2.1-rc.2",
118
+ "@deepseek-ai/dsh-session": "0.2.1-rc.2",
119
+ "@deepseek-ai/dsh-session-checkpoint-policy": "0.2.1-rc.2",
120
+ "@deepseek-ai/dsh-session-persistence": "0.2.1-rc.2",
121
+ "@deepseek-ai/dsh-session-projection": "0.2.1-rc.2",
122
+ "@deepseek-ai/dsh-system-prompt": "0.2.1-rc.2",
123
+ "@deepseek-ai/dsh-tools": "0.2.1-rc.2",
124
+ "@earendil-works/chord": "^1.4.0",
125
+ "@earendil-works/pi-ai": "^1.4.0",
126
+ "@earendil-works/pi-durable": "^1.4.0",
127
+ "@langchain/anthropic": "^1.5.11",
128
+ "@langchain/core": "^1.2.12",
129
+ "@langchain/langgraph": "^1.4.18",
130
+ "@langchain/langgraph-checkpoint": "^1.1.5",
131
+ "@langchain/openai": "^1.7.0",
132
+ "@opencode/core": "2.0.32",
133
+ "@opencode/plugin": "2.0.32",
134
+ "@opencode/sdk": "2.0.32",
135
+ "@opencode/server": "2.0.32",
136
+ "@restatedev/sessions": "^0.1.0",
137
+ "effect": "4.0.1",
138
+ "zod": "^4.6.5"
139
+ },
140
+ "peerDependenciesMeta": {
141
+ "@anthropic-ai/sdk": {
142
+ "optional": true
143
+ },
144
+ "@deepseek-ai/cordis": {
145
+ "optional": true
146
+ },
147
+ "@deepseek-ai/dsh-agent": {
148
+ "optional": true
149
+ },
150
+ "@deepseek-ai/dsh-agent-loop": {
151
+ "optional": true
152
+ },
153
+ "@deepseek-ai/dsh-llm": {
154
+ "optional": true
155
+ },
156
+ "@deepseek-ai/dsh-llm-deepseek-api-key": {
157
+ "optional": true
158
+ },
159
+ "@deepseek-ai/dsh-llm-pi-ai": {
160
+ "optional": true
161
+ },
162
+ "@deepseek-ai/dsh-session": {
163
+ "optional": true
164
+ },
165
+ "@deepseek-ai/dsh-session-checkpoint-policy": {
166
+ "optional": true
167
+ },
168
+ "@deepseek-ai/dsh-session-persistence": {
169
+ "optional": true
170
+ },
171
+ "@deepseek-ai/dsh-session-projection": {
172
+ "optional": true
173
+ },
174
+ "@deepseek-ai/dsh-system-prompt": {
175
+ "optional": true
176
+ },
177
+ "@deepseek-ai/dsh-tools": {
178
+ "optional": true
179
+ },
180
+ "@earendil-works/chord": {
181
+ "optional": true
182
+ },
183
+ "@earendil-works/pi-ai": {
184
+ "optional": true
185
+ },
186
+ "@earendil-works/pi-durable": {
187
+ "optional": true
188
+ },
189
+ "@langchain/anthropic": {
190
+ "optional": true
191
+ },
192
+ "@langchain/core": {
193
+ "optional": true
194
+ },
195
+ "@langchain/langgraph": {
196
+ "optional": true
197
+ },
198
+ "@langchain/langgraph-checkpoint": {
199
+ "optional": true
200
+ },
201
+ "@langchain/openai": {
202
+ "optional": true
203
+ },
204
+ "@opencode/core": {
205
+ "optional": true
206
+ },
207
+ "@opencode/plugin": {
208
+ "optional": true
209
+ },
210
+ "@opencode/sdk": {
211
+ "optional": true
212
+ },
213
+ "@opencode/server": {
214
+ "optional": true
215
+ },
216
+ "effect": {
217
+ "optional": true
218
+ }
219
+ },
220
+ "engines": {
221
+ "node": ">=24"
222
+ },
223
+ "scripts": {
224
+ "test": "vitest run",
225
+ "build:ts": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json"
226
+ }
6
227
  }