@restatedev/agents 0.4.0 → 0.6.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 (101) hide show
  1. package/README.md +104 -38
  2. package/dist/calls.d.ts +20 -1
  3. package/dist/calls.d.ts.map +1 -1
  4. package/dist/calls.js +46 -7
  5. package/dist/calls.js.map +1 -1
  6. package/dist/claude/index.d.ts +11 -11
  7. package/dist/claude/index.d.ts.map +1 -1
  8. package/dist/claude/index.js +9 -6
  9. package/dist/claude/index.js.map +1 -1
  10. package/dist/client.d.ts +1 -1
  11. package/dist/client.d.ts.map +1 -1
  12. package/dist/common.d.ts +62 -23
  13. package/dist/common.d.ts.map +1 -1
  14. package/dist/common.js +67 -38
  15. package/dist/common.js.map +1 -1
  16. package/dist/{signals.d.ts → companion.d.ts} +106 -43
  17. package/dist/companion.d.ts.map +1 -0
  18. package/dist/companion.js +463 -0
  19. package/dist/companion.js.map +1 -0
  20. package/dist/conversation.d.ts +115 -5
  21. package/dist/conversation.d.ts.map +1 -1
  22. package/dist/conversation.js +308 -79
  23. package/dist/conversation.js.map +1 -1
  24. package/dist/deepseek/index.d.ts +11 -11
  25. package/dist/deepseek/index.d.ts.map +1 -1
  26. package/dist/deepseek/index.js +23 -26
  27. package/dist/deepseek/index.js.map +1 -1
  28. package/dist/index.d.ts +8 -4
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +7 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/langgraph/checkpointer.d.ts +2 -0
  33. package/dist/langgraph/checkpointer.d.ts.map +1 -1
  34. package/dist/langgraph/checkpointer.js +1 -1
  35. package/dist/langgraph/checkpointer.js.map +1 -1
  36. package/dist/langgraph/graph.d.ts.map +1 -1
  37. package/dist/langgraph/graph.js +32 -13
  38. package/dist/langgraph/graph.js.map +1 -1
  39. package/dist/langgraph/index.d.ts +11 -11
  40. package/dist/langgraph/index.d.ts.map +1 -1
  41. package/dist/langgraph/index.js +19 -13
  42. package/dist/langgraph/index.js.map +1 -1
  43. package/dist/langgraph/nodes.d.ts +2 -1
  44. package/dist/langgraph/nodes.d.ts.map +1 -1
  45. package/dist/langgraph/nodes.js +23 -20
  46. package/dist/langgraph/nodes.js.map +1 -1
  47. package/dist/object.d.ts +24 -22
  48. package/dist/object.d.ts.map +1 -1
  49. package/dist/object.js +16 -11
  50. package/dist/object.js.map +1 -1
  51. package/dist/opencode/db.d.ts +81 -0
  52. package/dist/opencode/db.d.ts.map +1 -0
  53. package/dist/opencode/db.js +249 -0
  54. package/dist/opencode/db.js.map +1 -0
  55. package/dist/opencode/index.d.ts +184 -0
  56. package/dist/opencode/index.d.ts.map +1 -0
  57. package/dist/opencode/index.js +982 -0
  58. package/dist/opencode/index.js.map +1 -0
  59. package/dist/pi/index.d.ts +11 -11
  60. package/dist/pi/index.d.ts.map +1 -1
  61. package/dist/pi/index.js +16 -17
  62. package/dist/pi/index.js.map +1 -1
  63. package/dist/steering.d.ts +2 -1
  64. package/dist/steering.d.ts.map +1 -1
  65. package/dist/steering.js +2 -2
  66. package/dist/steering.js.map +1 -1
  67. package/dist/tools.d.ts +21 -0
  68. package/dist/tools.d.ts.map +1 -1
  69. package/dist/tools.js +54 -14
  70. package/dist/tools.js.map +1 -1
  71. package/dist/webhook.js +1 -1
  72. package/dist/webhook.js.map +1 -1
  73. package/package.json +37 -4
  74. package/src/calls.ts +52 -11
  75. package/src/claude/README.md +4 -2
  76. package/src/claude/index.ts +9 -7
  77. package/src/client.ts +1 -1
  78. package/src/common.ts +99 -40
  79. package/src/companion.ts +674 -0
  80. package/src/conversation.ts +377 -105
  81. package/src/deepseek/README.md +7 -1
  82. package/src/deepseek/index.ts +22 -24
  83. package/src/index.ts +10 -4
  84. package/src/langgraph/README.md +8 -5
  85. package/src/langgraph/checkpointer.ts +1 -1
  86. package/src/langgraph/graph.ts +30 -16
  87. package/src/langgraph/index.ts +24 -17
  88. package/src/langgraph/nodes.ts +24 -20
  89. package/src/object.ts +45 -40
  90. package/src/opencode/README.md +234 -0
  91. package/src/opencode/db.ts +387 -0
  92. package/src/opencode/index.ts +1243 -0
  93. package/src/pi/README.md +6 -1
  94. package/src/pi/index.ts +16 -15
  95. package/src/steering.ts +8 -7
  96. package/src/tools.ts +60 -18
  97. package/src/webhook.ts +1 -1
  98. package/dist/signals.d.ts.map +0 -1
  99. package/dist/signals.js +0 -383
  100. package/dist/signals.js.map +0 -1
  101. package/src/signals.ts +0 -590
@@ -0,0 +1,184 @@
1
+ import * as restate from '@restatedev/sessions';
2
+ import type { Plugin } from '@opencode/plugin';
3
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
4
+ import type { z } from 'zod';
5
+ import { type Check, type Configure } from '../common.ts';
6
+ import { type Call, type Items } from '../conversation.ts';
7
+ import { type Definition } from '../object.ts';
8
+ import { type Answer } from '../output.ts';
9
+ import { type Tool as AnyTool } from '../tools.ts';
10
+ import { type Host } from './db.ts';
11
+ /** An OpenCode model reference. */
12
+ export interface ModelRef {
13
+ providerID: string;
14
+ id: string;
15
+ }
16
+ /** An OpenCode tool of your own: `execute` gets the input and the tool call's id. */
17
+ export interface OpenCodeTool<Input = unknown> {
18
+ name: string;
19
+ description: string;
20
+ input: StandardSchemaV1<unknown, Input>;
21
+ execute(input: Input, callId: string): Promise<string>;
22
+ }
23
+ /** OpenCode's message id of prompt `id`: letters, digits, `_` and `-`. */
24
+ export declare const messageIdOf: (id: string) => string;
25
+ /** A tool call's result, as an item holds it. */
26
+ interface ItemResult {
27
+ id: string;
28
+ name: string;
29
+ output: string;
30
+ error?: boolean;
31
+ }
32
+ /**
33
+ * An item of the conversation: a user's message, an assistant's message (a
34
+ * step of OpenCode's: its text and tool calls), or tool calls' results. One
35
+ * copied from OpenCode (a message steered to the turn, say) has `from`: the
36
+ * id of its OpenCode message, and for a result the id of its part too
37
+ * (`<message>/<part>`), by which it is copied once.
38
+ */
39
+ export type Item = {
40
+ type: 'user';
41
+ text: string;
42
+ from?: string;
43
+ } | {
44
+ type: 'assistant';
45
+ text: string;
46
+ calls?: Call[];
47
+ from?: string;
48
+ } | {
49
+ type: 'results';
50
+ results: ItemResult[];
51
+ from?: string;
52
+ };
53
+ /**
54
+ * OpenCode's items. A user's message is said by the user; an assistant's
55
+ * says its text, with its tool calls; results are results. OpenCode's
56
+ * synthetic messages, idle markers and compactions are never copied.
57
+ */
58
+ export declare const items: Items<Item>;
59
+ /** What `work` gets: `prompt(text)`, and the OpenCode host for anything else. */
60
+ export interface OpenCodeSession {
61
+ /** OpenCode, booted on its first use. */
62
+ host: () => Promise<Host>;
63
+ /**
64
+ * Answer `text`, once, with the answer's text: a turn of the object's
65
+ * conversation (each prompt of an invocation has an id of its own, the
66
+ * same on every attempt). With `check`, a reply it returns a message for
67
+ * is asked again within the turn, once. With `reminder`, the model is told
68
+ * it after the prompt, which the conversation keeps as it was asked.
69
+ */
70
+ prompt: (text: string, check?: Check, reminder?: string) => Promise<string>;
71
+ }
72
+ /** An agent's options: `chat`, `prompt` and `withOpenCode` take them. */
73
+ export interface Options<O extends z.ZodObject | undefined = z.ZodObject | undefined> {
74
+ /** The model of the session, as `provider/model-id`: MODEL, or else openai/gpt-6-sol. */
75
+ model?: string | ModelRef;
76
+ /** The system prompt: who the agent is, and how it answers. */
77
+ system?: string;
78
+ /** The agent's tools, which replace OpenCode's own: `tool()`s, Restate's (`call`, `signal`...), or OpenCode tools of your own. */
79
+ tools?: (AnyTool | OpenCodeTool)[];
80
+ /**
81
+ * A directory of the local machine, made if missing: OpenCode keeps its own
82
+ * tools (read, write, edit, shell, glob, grep...) there, besides `tools`,
83
+ * and the session works in it. Or a function of the invocation's context
84
+ * that gives one: `(ctx) => \`/work/${ctx.key}\``.
85
+ */
86
+ workspace?: string | ((ctx: restate.SessionContext) => string | Promise<string>);
87
+ /** OpenCode plugins, such as the one of the model. */
88
+ plugins?: Plugin.Plugin[];
89
+ /**
90
+ * How OpenCode asks the model again after a request failed for a reason
91
+ * that passes (an overloaded or unreachable provider, a rate limit), as pi
92
+ * does: by default without limit, 2 s after the first failure, then twice
93
+ * as long each time, a minute apart at most (`baseDelayMs`,
94
+ * `maxAgentDelayMs`), but never before the provider said to try again
95
+ * (its `retry-after`, which the cap does not shorten: up to 15 minutes, as
96
+ * OpenCode takes it). A request refused for good (a wrong API key, an
97
+ * unknown model, a request a retry would make again) is never asked again,
98
+ * and ends the turn (424). Unlike pi, OpenCode asks again 10 times at most
99
+ * in a step of the turn, and after its 4th timeout in a step not at all:
100
+ * without `maxRetries`, an outage OpenCode stops asking again about fails
101
+ * the attempt instead, and Restate's retry of it resumes the turn, waits
102
+ * from 2 s again. With `maxRetries`, a failure after that many retries of
103
+ * a step ends the turn, as one OpenCode stops asking again about does.
104
+ * `enabled: false` asks no request again.
105
+ */
106
+ retry?: Partial<RetryPolicy>;
107
+ /**
108
+ * With a workspace, OpenCode's own tools ask before what its permission
109
+ * rules say to ask about (by default, a read of a `.env` file). Without
110
+ * `approval`, nobody answers such an ask: it is denied, as OpenCode denies
111
+ * one nobody is present for, and the model gets the denial as the tool's
112
+ * error. With `approval`, the tool call waits for the signal
113
+ * {@link APPROVAL} to the agent, which `pending` shows with the call's tool
114
+ * and arguments and the permission asked (`action`, `resources`); `true`,
115
+ * or a text such as "approved" or "yes", allows it, anything else denies
116
+ * it (a text, or `{ reason }`, is the denial's reason, for the model).
117
+ */
118
+ approval?: boolean;
119
+ /**
120
+ * The answer as data: a zod object, whose JSON the agent replies with
121
+ * (checked), and `chat` and `prompt` answer with (see output.ts).
122
+ */
123
+ output?: O;
124
+ }
125
+ /** How a failed model request is asked again ({@link Options.retry}): pi's policy, as OpenCode can take it. */
126
+ export interface RetryPolicy {
127
+ enabled: boolean;
128
+ maxRetries: number;
129
+ baseDelayMs: number;
130
+ maxAgentDelayMs?: number;
131
+ }
132
+ /** The signal that an OpenCode tool call waits for, to the agent, when OpenCode asks for a permission (`approval`). */
133
+ export declare const APPROVAL = "approval";
134
+ /**
135
+ * Run `work(opencode)` with OpenCode over the object's database: `model` is
136
+ * the session's model, `plugins` OpenCode plugins (such as the model's), and
137
+ * `tools` the agent's tools. `opencode` has `prompt(text)`, and the
138
+ * OpenCode host for anything else.
139
+ */
140
+ export declare const withOpenCode: <T>(ctx: restate.SessionContext, { plugins, model: named, system, tools, workspace: where, retry, approval }: Options, work: (opencode: OpenCodeSession) => Promise<T>) => Promise<T>;
141
+ /**
142
+ * Answer `text`, once: a retried attempt resumes the same prompt. Each prompt
143
+ * of an invocation is a prompt of its own. With `output`, it answers with the
144
+ * value the agent replies with (see output.ts), not its text.
145
+ */
146
+ export declare const prompt: <O extends z.ZodObject | undefined = undefined>(ctx: restate.SessionContext, text: string, options?: Options<O>) => Promise<Answer<O>>;
147
+ /**
148
+ * A chat handler: each message an invocation, the session in the object's
149
+ * database, with `options`, or the options a function of the invocation's
150
+ * context gives (each user's workspace, say). It takes the message as plain
151
+ * text (`curl -d 'Hi'`) or as a JSON string (`curl --json '"Hi"'`).
152
+ */
153
+ export declare const chat: <O extends z.ZodObject | undefined = undefined>(options?: Configure<Options<O>>) => {
154
+ input: restate.Serde<string>;
155
+ accept: string;
156
+ explicitCancellation: boolean;
157
+ handler: (ctx: import("../common.ts").AgentContext, text: string) => Promise<Answer<O>>;
158
+ };
159
+ /**
160
+ * A view of the conversation as clients read it: a page at a time, or what
161
+ * is new or changed since a cursor; `view: 'summary'` for each turn's
162
+ * message and answer. It answers while a message is answered. `clear` also
163
+ * empties OpenCode's database.
164
+ */
165
+ export declare const messages: () => (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
166
+ /** An agent: a virtual object `name`, one per key, whose `chat` handler is {@link chat}, and `messages` {@link messages}. */
167
+ export declare const agent: <P extends string, O extends z.ZodObject | undefined = undefined>({ name, description, schema, background, window, budget, ...options }: Options<O> & Definition<P>) => restate.VirtualObjectDefinition<P, {
168
+ signal: (ctx: import("../common.ts").AgentViewContext, signal: import("../companion.ts").Signal) => Promise<import("../companion.ts").Resolved>;
169
+ pending: (ctx: import("../common.ts").AgentViewContext) => Promise<import("../companion.ts").Waiting[]>;
170
+ status: (ctx: import("../common.ts").AgentViewContext) => Promise<import("../object.ts").Status>;
171
+ stop: (ctx: import("../common.ts").AgentViewContext) => Promise<import("../object.ts").Stopped>;
172
+ notifications: (ctx: import("../common.ts").AgentViewContext) => Promise<import("../companion.ts").Notifications>;
173
+ watch: (ctx: import("../common.ts").AgentViewContext, watch: import("../companion.ts").Watch) => Promise<import("../companion.ts").Notifications>;
174
+ } & {
175
+ clear: (ctx: import("../common.ts").AgentViewContext) => Promise<import("../object.ts").Stopped>;
176
+ steer: (ctx: import("../common.ts").AgentViewContext, text: string) => Promise<import("../companion.ts").Steered>;
177
+ sync: (ctx: import("../common.ts").AgentViewContext, { cursor, revision, timeoutSeconds, limit, view }?: import("../object.ts").SyncRequest) => Promise<import("../object.ts").Synced>;
178
+ } & {
179
+ chat: (ctx: import("../common.ts").AgentContext, text: string) => Promise<Answer<O>>;
180
+ } & {
181
+ messages: (ctx: restate.ViewContext, request?: import("../common.ts").MessagesRequest) => Promise<import("../common.ts").Messages>;
182
+ }>;
183
+ export {};
184
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/opencode/index.ts"],"names":[],"mappings":"AA0EA,OAAO,KAAK,OAAO,MAAM,sBAAsB,CAAC;AAGhD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,EAIL,KAAK,KAAK,EAEV,KAAK,SAAS,EAWf,MAAM,cAAc,CAAC;AACtB,OAAO,EAA4B,KAAK,IAAI,EAAE,KAAK,KAAK,EAAiC,MAAM,oBAAoB,CAAC;AAGpH,OAAO,EAAE,KAAK,UAAU,EAA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