@getmodus/sdk 0.9.0 → 0.10.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/CHANGELOG.md +163 -0
- package/README.md +182 -14
- package/dist/{conversations-D7ffhHIh.d.cts → _client-base-H5rt5EGy.d.cts} +3355 -868
- package/dist/{conversations-D7ffhHIh.d.ts → _client-base-H5rt5EGy.d.ts} +3355 -868
- package/dist/{chunk-SP4UYRYG.js → chunk-BMYCORZF.js} +601 -304
- package/dist/chunk-BMYCORZF.js.map +1 -0
- package/dist/chunk-NA3ZD35T.cjs +2666 -0
- package/dist/chunk-NA3ZD35T.cjs.map +1 -0
- package/dist/index.cjs +572 -2816
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +716 -552
- package/dist/index.d.ts +716 -552
- package/dist/index.js +399 -294
- package/dist/index.js.map +1 -1
- package/dist/management/index.cjs +555 -2659
- package/dist/management/index.cjs.map +1 -1
- package/dist/management/index.d.cts +211 -42
- package/dist/management/index.d.ts +211 -42
- package/dist/management/index.js +291 -98
- package/dist/management/index.js.map +1 -1
- package/package.json +8 -1
- package/dist/chunk-SP4UYRYG.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
APIConnectionError,
|
|
3
3
|
AuthenticationError,
|
|
4
|
+
BadRequestError,
|
|
4
5
|
ConflictError,
|
|
5
6
|
CustomContextItemsResource,
|
|
6
7
|
DEFAULT_MAX_PAGE_SIZE,
|
|
@@ -26,10 +27,11 @@ import {
|
|
|
26
27
|
normalizePageToken,
|
|
27
28
|
omitUndefined,
|
|
28
29
|
operationBaseUrl,
|
|
30
|
+
requireAgentHost,
|
|
29
31
|
validateChatModel,
|
|
30
32
|
validateId,
|
|
31
33
|
validatePageSize
|
|
32
|
-
} from "./chunk-
|
|
34
|
+
} from "./chunk-BMYCORZF.js";
|
|
33
35
|
|
|
34
36
|
// src/_agent_run_request.ts
|
|
35
37
|
import { randomUUID } from "crypto";
|
|
@@ -39,17 +41,6 @@ function resolveAgentRunSessionId(sessionId) {
|
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
// src/_streaming.ts
|
|
42
|
-
var SILENTLY_IGNORED = /* @__PURE__ */ new Set([
|
|
43
|
-
"heartbeat",
|
|
44
|
-
"ws_chunk",
|
|
45
|
-
"unknown",
|
|
46
|
-
"tool_start",
|
|
47
|
-
"tool_end",
|
|
48
|
-
"tool_chunk",
|
|
49
|
-
"agent_thinking",
|
|
50
|
-
"warning",
|
|
51
|
-
"selected_final_content"
|
|
52
|
-
]);
|
|
53
44
|
function parseLine(line) {
|
|
54
45
|
if (!line.startsWith("data:")) return void 0;
|
|
55
46
|
const raw = line.slice(5).trimStart();
|
|
@@ -67,30 +58,61 @@ function parseLine(line) {
|
|
|
67
58
|
function isAsyncIterable(value) {
|
|
68
59
|
return Symbol.asyncIterator in Object(value);
|
|
69
60
|
}
|
|
70
|
-
|
|
61
|
+
var NODE_TERMINAL_TYPES = /* @__PURE__ */ new Set(["done", "error", "cancelled", "stream_timeout"]);
|
|
62
|
+
function* parseData(data, id) {
|
|
71
63
|
const eventType = typeof data.type === "string" ? data.type : "unknown";
|
|
72
|
-
|
|
64
|
+
const withId = id === void 0 ? {} : { id };
|
|
65
|
+
if (eventType === "node_content") {
|
|
66
|
+
const inner = data.event;
|
|
67
|
+
if (inner && typeof inner === "object" && !Array.isArray(inner)) {
|
|
68
|
+
const frame = inner;
|
|
69
|
+
if (!NODE_TERMINAL_TYPES.has(frame.type)) yield* parseData(frame, id);
|
|
70
|
+
}
|
|
71
|
+
} else if (eventType === "token") {
|
|
73
72
|
const event = {
|
|
74
73
|
type: "token",
|
|
75
|
-
content: typeof data.content === "string" ? data.content : ""
|
|
74
|
+
content: typeof data.content === "string" ? data.content : "",
|
|
75
|
+
...typeof data.subAgent === "string" ? { subAgent: data.subAgent } : {},
|
|
76
|
+
...withId
|
|
76
77
|
};
|
|
77
78
|
yield event;
|
|
78
|
-
} else if (eventType === "done") {
|
|
79
|
+
} else if (eventType === "done" || eventType === "workflow_done") {
|
|
80
|
+
const content = eventType === "done" ? data.content : data.finalOutput;
|
|
79
81
|
const event = {
|
|
80
82
|
type: "done",
|
|
81
83
|
runId: typeof data.runId === "string" ? data.runId : "",
|
|
82
|
-
threadId: typeof data.threadId === "string" ? data.threadId : ""
|
|
84
|
+
threadId: typeof data.threadId === "string" ? data.threadId : "",
|
|
85
|
+
...typeof content === "string" ? { content } : {},
|
|
86
|
+
...withId
|
|
83
87
|
};
|
|
84
88
|
yield event;
|
|
85
|
-
} else if (eventType === "
|
|
89
|
+
} else if (eventType === "approval_required") {
|
|
90
|
+
const event = { type: "approval_required" };
|
|
91
|
+
for (const key of ["toolId", "toolName", "title", "description", "nodeId", "prompt"]) {
|
|
92
|
+
const value = data[key];
|
|
93
|
+
if (typeof value === "string") event[key] = value;
|
|
94
|
+
}
|
|
95
|
+
const { args, options } = data;
|
|
96
|
+
if (args && typeof args === "object" && !Array.isArray(args)) event.args = args;
|
|
97
|
+
if (Array.isArray(options)) event.options = options;
|
|
98
|
+
yield { ...event, ...withId };
|
|
99
|
+
} else if (eventType === "workflow_cancelled") {
|
|
100
|
+
const event = { type: "cancelled", ...withId };
|
|
101
|
+
yield event;
|
|
102
|
+
} else if (eventType === "error" || eventType === "workflow_error") {
|
|
86
103
|
const msg = data.error ?? data.message ?? data.content ?? "Unknown run error";
|
|
87
|
-
const event = {
|
|
104
|
+
const event = {
|
|
105
|
+
type: "error",
|
|
106
|
+
message: String(msg),
|
|
107
|
+
...typeof data.requestId === "string" ? { requestId: data.requestId } : {},
|
|
108
|
+
...withId
|
|
109
|
+
};
|
|
88
110
|
yield event;
|
|
89
111
|
} else if (eventType === "cancelled") {
|
|
90
|
-
const event = { type: "cancelled" };
|
|
112
|
+
const event = { type: "cancelled", ...withId };
|
|
91
113
|
yield event;
|
|
92
114
|
} else if (eventType === "stream_timeout") {
|
|
93
|
-
const event = { type: "stream_timeout" };
|
|
115
|
+
const event = { type: "stream_timeout", ...withId };
|
|
94
116
|
yield event;
|
|
95
117
|
} else if (eventType === "assistant_content_reset") {
|
|
96
118
|
if (typeof data.content !== "string") return;
|
|
@@ -99,24 +121,31 @@ function* parseData(data) {
|
|
|
99
121
|
content: data.content,
|
|
100
122
|
...typeof data.visibleContent === "string" ? { visibleContent: data.visibleContent } : {},
|
|
101
123
|
attempt: typeof data.attempt === "number" ? data.attempt : 1,
|
|
102
|
-
reason: "provider_stream_failed"
|
|
124
|
+
reason: "provider_stream_failed",
|
|
125
|
+
...data.subAgent && typeof data.subAgent === "object" && !Array.isArray(data.subAgent) ? { subAgent: data.subAgent } : {},
|
|
126
|
+
...withId
|
|
103
127
|
};
|
|
104
128
|
yield event;
|
|
105
|
-
} else if (!SILENTLY_IGNORED.has(eventType)) {
|
|
106
|
-
return;
|
|
107
129
|
}
|
|
108
130
|
}
|
|
109
|
-
function
|
|
110
|
-
|
|
131
|
+
function sseLineParser() {
|
|
132
|
+
let lastId;
|
|
133
|
+
return function* (line) {
|
|
134
|
+
if (line.startsWith("id:")) {
|
|
135
|
+
lastId = line.slice(3).trim() || void 0;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
111
138
|
const data = parseLine(line);
|
|
112
|
-
if (data !== void 0) yield* parseData(data);
|
|
113
|
-
}
|
|
139
|
+
if (data !== void 0) yield* parseData(data, lastId);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function* parseSyncSseLines(lines) {
|
|
143
|
+
const parse = sseLineParser();
|
|
144
|
+
for (const line of lines) yield* parse(line);
|
|
114
145
|
}
|
|
115
146
|
async function* parseAsyncSseLines(lines) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (data !== void 0) yield* parseData(data);
|
|
119
|
-
}
|
|
147
|
+
const parse = sseLineParser();
|
|
148
|
+
for await (const line of lines) yield* parse(line);
|
|
120
149
|
}
|
|
121
150
|
function parseSseStream(lines) {
|
|
122
151
|
return isAsyncIterable(lines) ? parseAsyncSseLines(lines) : parseSyncSseLines(lines);
|
|
@@ -124,174 +153,199 @@ function parseSseStream(lines) {
|
|
|
124
153
|
|
|
125
154
|
// src/_chat.ts
|
|
126
155
|
function effectiveThreadId(threadId) {
|
|
127
|
-
if (threadId === void 0) return void 0;
|
|
128
|
-
if (!threadId.trim()) return void 0;
|
|
156
|
+
if (threadId === void 0 || !threadId.trim()) return void 0;
|
|
129
157
|
return threadId;
|
|
130
158
|
}
|
|
131
|
-
function
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
function chatPath(resource, resourceId, options) {
|
|
135
|
-
const safeId = encodeURIComponent(String(resourceId));
|
|
136
|
-
const tid = effectiveThreadId(options.threadId);
|
|
137
|
-
if (tid !== void 0) {
|
|
138
|
-
const safeTid = encodeURIComponent(tid);
|
|
139
|
-
return `/api/v1/${resource}/${safeId}/conversations/${safeTid}/chat`;
|
|
140
|
-
}
|
|
141
|
-
return `/api/v1/${resource}/${safeId}/chat`;
|
|
142
|
-
}
|
|
143
|
-
function modusChatPath(options) {
|
|
144
|
-
const tid = effectiveThreadId(options.threadId);
|
|
145
|
-
if (tid !== void 0) {
|
|
146
|
-
validateId(tid, "thread_id");
|
|
147
|
-
return `/api/v1/modus/conversations/${encodeURIComponent(tid)}/chat`;
|
|
148
|
-
}
|
|
149
|
-
return `/api/v1/modus/chat`;
|
|
150
|
-
}
|
|
151
|
-
function agentHostRunsPath(resource, resourceId) {
|
|
152
|
-
if (resource === "modus") return "/agent/v1/modus/runs";
|
|
153
|
-
const safeId = encodeURIComponent(String(resourceId));
|
|
154
|
-
return `/agent/v1/${resource}/${safeId}/runs`;
|
|
155
|
-
}
|
|
156
|
-
function chatBody(message, model) {
|
|
157
|
-
return { message, model };
|
|
158
|
-
}
|
|
159
|
-
function agentRunsBody(message, model, options) {
|
|
160
|
-
const body = {
|
|
161
|
-
message,
|
|
162
|
-
config: { model },
|
|
163
|
-
sessionId: options.sessionId
|
|
164
|
-
};
|
|
165
|
-
if (options.organizationId !== void 0) {
|
|
166
|
-
body.organizationId = options.organizationId;
|
|
167
|
-
}
|
|
168
|
-
if (options.version !== void 0) {
|
|
169
|
-
body.version = options.version;
|
|
170
|
-
}
|
|
171
|
-
return body;
|
|
159
|
+
function runsPath(target) {
|
|
160
|
+
if (target.kind === "modus") return "/agent/v1/modus/runs";
|
|
161
|
+
return `/agent/v1/${target.kind}/${encodeURIComponent(String(target.id))}/runs`;
|
|
172
162
|
}
|
|
173
163
|
function raiseForEvent(event) {
|
|
174
|
-
if (event.type === "error") throw new ModusError(event.message);
|
|
164
|
+
if (event.type === "error") throw new ModusError(event.message, { requestId: event.requestId });
|
|
175
165
|
if (event.type === "cancelled") throw new RunCancelledError();
|
|
176
166
|
if (event.type === "stream_timeout") throw new StreamTimeoutError();
|
|
177
167
|
return event;
|
|
178
168
|
}
|
|
179
|
-
var
|
|
180
|
-
|
|
169
|
+
var ChatSession = class _ChatSession {
|
|
170
|
+
/**
|
|
171
|
+
* @internal A session built with `failure` never sends; every read rejects
|
|
172
|
+
* with it, as the promise `chat()` used to return did.
|
|
173
|
+
*/
|
|
174
|
+
constructor(http, request, failure) {
|
|
181
175
|
this.http = http;
|
|
182
|
-
this.
|
|
183
|
-
this.
|
|
184
|
-
this.
|
|
185
|
-
|
|
176
|
+
this.request = request;
|
|
177
|
+
this.received = [];
|
|
178
|
+
this.finished = false;
|
|
179
|
+
if (failure !== void 0) {
|
|
180
|
+
this.failure = failure;
|
|
181
|
+
this.finished = true;
|
|
182
|
+
}
|
|
186
183
|
}
|
|
187
184
|
/**
|
|
188
|
-
*
|
|
185
|
+
* The assistant's reply as text chunks, as they arrive. Sub-agent output is
|
|
186
|
+
* left out.
|
|
189
187
|
*
|
|
190
|
-
* @
|
|
191
|
-
* @throws {ModusError} When the stream emits an error event.
|
|
188
|
+
* @throws {ModusError} When the run reports an error.
|
|
192
189
|
* @throws {RunCancelledError} When the run is cancelled.
|
|
193
|
-
* @throws {StreamTimeoutError} When the
|
|
190
|
+
* @throws {StreamTimeoutError} When the run times out.
|
|
194
191
|
*/
|
|
195
192
|
async *textStream() {
|
|
196
|
-
|
|
197
|
-
|
|
193
|
+
const lastReset = this.received.findLastIndex(
|
|
194
|
+
(event) => event.type === "assistant_content_reset" && event.subAgent === void 0
|
|
195
|
+
);
|
|
196
|
+
let index = 0;
|
|
197
|
+
for await (const event of this.replay(false)) {
|
|
198
|
+
const at = index++;
|
|
199
|
+
if (at < lastReset) continue;
|
|
200
|
+
if (at === lastReset && event.type === "assistant_content_reset") {
|
|
201
|
+
if (event.content) yield event.content;
|
|
202
|
+
} else if (event.type === "token" && event.subAgent === void 0) {
|
|
203
|
+
yield event.content;
|
|
204
|
+
}
|
|
198
205
|
}
|
|
199
206
|
}
|
|
200
207
|
/**
|
|
201
|
-
*
|
|
208
|
+
* Every run event (tokens, approvals, `done`, …) as it arrives.
|
|
202
209
|
*
|
|
203
|
-
* @
|
|
204
|
-
* @throws {ModusError} When the stream emits an error event.
|
|
210
|
+
* @throws {ModusError} When the run reports an error.
|
|
205
211
|
* @throws {RunCancelledError} When the run is cancelled.
|
|
206
|
-
* @throws {StreamTimeoutError} When the
|
|
212
|
+
* @throws {StreamTimeoutError} When the run times out.
|
|
207
213
|
*/
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (normalized.type === "assistant_content_reset") {
|
|
220
|
-
this.textParts.length = 0;
|
|
221
|
-
this.textParts.push(normalized.content);
|
|
222
|
-
}
|
|
223
|
-
if (normalized.type === "done") {
|
|
224
|
-
this.final = {
|
|
225
|
-
content: this.textParts.join(""),
|
|
226
|
-
threadId: normalized.threadId,
|
|
227
|
-
runId: normalized.runId
|
|
228
|
-
};
|
|
229
|
-
yield normalized;
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
yield normalized;
|
|
233
|
-
}
|
|
214
|
+
eventStream() {
|
|
215
|
+
return this.replay(true);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Waits for the run to finish and returns its final answer. `approval` is
|
|
219
|
+
* set when the run stopped for a decision; continue with {@link resume}.
|
|
220
|
+
*
|
|
221
|
+
* @throws {ModusError} When the run reports an error or ends without finishing.
|
|
222
|
+
*/
|
|
223
|
+
async result() {
|
|
224
|
+
for await (const _event of this.replay(true)) {
|
|
234
225
|
}
|
|
226
|
+
if (this.final === void 0) throw new ModusError("Chat stream ended without a done event");
|
|
227
|
+
return this.final;
|
|
228
|
+
}
|
|
229
|
+
/** Resolve with the final {@link ChatResult}, so the session can be awaited. */
|
|
230
|
+
then(onfulfilled, onrejected) {
|
|
231
|
+
return this.result().then(onfulfilled, onrejected);
|
|
232
|
+
}
|
|
233
|
+
/** Handle a failed run, as on the promise `chat()` used to return. */
|
|
234
|
+
catch(onrejected) {
|
|
235
|
+
return this.result().catch(onrejected);
|
|
236
|
+
}
|
|
237
|
+
/** Run `onfinally` once the run settles, as on the promise `chat()` used to return. */
|
|
238
|
+
finally(onfinally) {
|
|
239
|
+
return this.result().finally(onfinally);
|
|
235
240
|
}
|
|
236
241
|
/**
|
|
237
|
-
*
|
|
242
|
+
* Approve or deny the action this run stopped for, continuing the same
|
|
243
|
+
* conversation.
|
|
238
244
|
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
245
|
+
* @param input.decision - `approve` or `deny`.
|
|
246
|
+
* @returns A new session for the continued run.
|
|
247
|
+
* @throws {ModusError} When called before the run has finished.
|
|
241
248
|
*
|
|
242
|
-
* @
|
|
243
|
-
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```ts
|
|
251
|
+
* const chat = client.modus.chat('Fix the typo in accounts', { model: 'claude-sonnet-5.5' })
|
|
252
|
+
* const first = await chat
|
|
253
|
+
* if (first.approval) {
|
|
254
|
+
* const done = await chat.resume({ decision: 'approve' })
|
|
255
|
+
* }
|
|
256
|
+
* ```
|
|
244
257
|
*/
|
|
245
|
-
|
|
258
|
+
resume(input) {
|
|
246
259
|
if (this.final === void 0) {
|
|
247
|
-
throw new ModusError(
|
|
248
|
-
|
|
249
|
-
|
|
260
|
+
throw new ModusError("resume() needs a finished run: await the chat first");
|
|
261
|
+
}
|
|
262
|
+
const { runId, threadId } = this.final;
|
|
263
|
+
validateId(runId, "run_id");
|
|
264
|
+
return new _ChatSession(this.http, {
|
|
265
|
+
path: `/agent/v1/runs/${encodeURIComponent(runId)}/resume`,
|
|
266
|
+
body: { sessionId: threadId, decision: input.decision },
|
|
267
|
+
sessionId: threadId,
|
|
268
|
+
baseUrl: this.request.baseUrl
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Stop reading the run and release its connection. Call it when you stop
|
|
273
|
+
* iterating before the run ends; a session that reached `done` has already
|
|
274
|
+
* let go. The run itself keeps going on the server.
|
|
275
|
+
*/
|
|
276
|
+
async close() {
|
|
277
|
+
this.finished = true;
|
|
278
|
+
await this.live?.return(void 0);
|
|
279
|
+
}
|
|
280
|
+
// ponytail: one consumer at a time; concurrent iterators would race on `live`.
|
|
281
|
+
async *replay(resetAware) {
|
|
282
|
+
let index = 0;
|
|
283
|
+
while (true) {
|
|
284
|
+
if (index < this.received.length) {
|
|
285
|
+
yield this.received[index++];
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (this.failure !== void 0) throw this.failure;
|
|
289
|
+
if (this.finished) return;
|
|
290
|
+
this.live ?? (this.live = this.open(resetAware));
|
|
291
|
+
try {
|
|
292
|
+
const next = await this.live.next();
|
|
293
|
+
if (next.done) {
|
|
294
|
+
this.finished = true;
|
|
295
|
+
} else {
|
|
296
|
+
this.received.push(next.value);
|
|
297
|
+
if (next.value.type === "done") await this.close();
|
|
298
|
+
}
|
|
299
|
+
} catch (error) {
|
|
300
|
+
this.failure = error;
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
async *open(resetAware) {
|
|
306
|
+
const request = this.request;
|
|
307
|
+
const body = resetAware ? { ...request.body, streamProtocolVersion: 2 } : request.body;
|
|
308
|
+
const lines = this.http.streamPost(request.path, body, { baseUrl: request.baseUrl });
|
|
309
|
+
for await (const event of parseSseStream(lines)) {
|
|
310
|
+
raiseForEvent(event);
|
|
311
|
+
if (event.type === "approval_required") this.approval = event;
|
|
312
|
+
if (event.type === "done") {
|
|
313
|
+
this.final = {
|
|
314
|
+
content: event.content ?? "",
|
|
315
|
+
threadId: event.threadId || this.request.sessionId,
|
|
316
|
+
runId: event.runId,
|
|
317
|
+
...this.approval !== void 0 ? { approval: this.approval } : {}
|
|
318
|
+
};
|
|
319
|
+
yield event;
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
yield event;
|
|
250
323
|
}
|
|
251
|
-
return this.final;
|
|
252
324
|
}
|
|
253
325
|
};
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
const data = await http.post(path, chatBody(message, options.model));
|
|
261
|
-
return data;
|
|
262
|
-
}
|
|
263
|
-
function chatStreamSession(http, config, resource, resourceId, message, options) {
|
|
264
|
-
validateId(resourceId, `${resource}_id`);
|
|
265
|
-
validateChatModel(options.model);
|
|
266
|
-
const path = agentHostRunsPath(resource, resourceId);
|
|
267
|
-
return new ChatStream(
|
|
268
|
-
http,
|
|
269
|
-
path,
|
|
270
|
-
agentRunsBody(message, options.model, {
|
|
271
|
-
sessionId: resolveSessionId(options.threadId),
|
|
272
|
-
organizationId: config.organizationId,
|
|
273
|
-
version: options.version
|
|
274
|
-
}),
|
|
275
|
-
config.agentHost
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
async function modusChatBuffered(http, message, options) {
|
|
279
|
-
validateChatModel(options.model);
|
|
280
|
-
const path = modusChatPath({ threadId: options.threadId });
|
|
281
|
-
const data = await http.post(path, chatBody(message, options.model));
|
|
282
|
-
return data;
|
|
326
|
+
function startChat(http, config, target, message, options) {
|
|
327
|
+
try {
|
|
328
|
+
return new ChatSession(http, chatRequest(config, target, message, options));
|
|
329
|
+
} catch (error) {
|
|
330
|
+
return new ChatSession(http, void 0, error);
|
|
331
|
+
}
|
|
283
332
|
}
|
|
284
|
-
function
|
|
333
|
+
function chatRequest(config, target, message, options) {
|
|
334
|
+
if (target.kind !== "modus") validateId(target.id, `${target.kind.slice(0, -1)}_id`);
|
|
285
335
|
validateChatModel(options.model);
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
336
|
+
const baseUrl = requireAgentHost(config);
|
|
337
|
+
const sessionId = resolveAgentRunSessionId(effectiveThreadId(options.threadId));
|
|
338
|
+
return {
|
|
339
|
+
path: runsPath(target),
|
|
340
|
+
body: {
|
|
341
|
+
message,
|
|
342
|
+
config: { model: options.model },
|
|
343
|
+
sessionId,
|
|
344
|
+
...config.organizationId !== void 0 ? { organizationId: config.organizationId } : {}
|
|
345
|
+
},
|
|
346
|
+
sessionId,
|
|
347
|
+
baseUrl
|
|
348
|
+
};
|
|
295
349
|
}
|
|
296
350
|
|
|
297
351
|
// src/types/workflows.ts
|
|
@@ -329,8 +383,9 @@ var WorkflowsResourceBase = class {
|
|
|
329
383
|
* for the first page.
|
|
330
384
|
* @param options.search - Case-insensitive substring filter on workflow name.
|
|
331
385
|
* @param options.type - Filter by workflow type.
|
|
332
|
-
* @param options.view - Variation view. `active` reads deployed
|
|
333
|
-
* snapshot.
|
|
386
|
+
* @param options.view - Variation view. `active` reads the deployed version. `draft` reads the
|
|
387
|
+
* latest builder snapshot. `published` reads only a published workflow of type workflow that
|
|
388
|
+
* you can use; tasks are excluded.
|
|
334
389
|
* @param options.includeVariation - Include the requested variation payload (`trigger`,
|
|
335
390
|
* `workflowStructure`, `agentSelection`) on list rows.
|
|
336
391
|
* @returns A page of workflows.
|
|
@@ -421,6 +476,11 @@ function randomRunId() {
|
|
|
421
476
|
function sessionIdFromBody(body) {
|
|
422
477
|
return resolveAgentRunSessionId(body.sessionId);
|
|
423
478
|
}
|
|
479
|
+
function requireSessionId(sessionId) {
|
|
480
|
+
if (!sessionId?.trim()) {
|
|
481
|
+
throw new ValidationError("sessionId must be the run's conversation id (a non-empty string)");
|
|
482
|
+
}
|
|
483
|
+
}
|
|
424
484
|
function makeAgentRunStream(runId, events) {
|
|
425
485
|
return {
|
|
426
486
|
runId,
|
|
@@ -439,7 +499,7 @@ var WorkflowRunsResource = class {
|
|
|
439
499
|
/**
|
|
440
500
|
* List runs for a workflow.
|
|
441
501
|
*
|
|
442
|
-
* @param workflowId -
|
|
502
|
+
* @param workflowId - Numeric workflow id.
|
|
443
503
|
* @param options.pageSize - Maximum items per page (default 25, max 100).
|
|
444
504
|
* @param options.pageToken - Opaque token from a previous page.
|
|
445
505
|
* @param options.status - Filter by run status.
|
|
@@ -494,7 +554,7 @@ var WorkflowRunsResource = class {
|
|
|
494
554
|
/**
|
|
495
555
|
* Retrieve a workflow run by id.
|
|
496
556
|
*
|
|
497
|
-
* @param workflowId -
|
|
557
|
+
* @param workflowId - Numeric workflow id the run belongs to.
|
|
498
558
|
* @param runId - Run id (the list row's `workflowId` field, not the composite `id`).
|
|
499
559
|
* @param options.temporalRunId - Optional internal execution id when disambiguating retries.
|
|
500
560
|
* @returns Run details including status and output.
|
|
@@ -508,7 +568,7 @@ var WorkflowRunsResource = class {
|
|
|
508
568
|
*/
|
|
509
569
|
async get(workflowId, runId, options = {}) {
|
|
510
570
|
validateId(workflowId, "workflow_id");
|
|
511
|
-
if (!runId.trim()) throw new
|
|
571
|
+
if (!runId.trim()) throw new ValidationError("run_id must be a non-empty string");
|
|
512
572
|
const query = options.temporalRunId !== void 0 ? { temporalRunId: options.temporalRunId } : void 0;
|
|
513
573
|
const data = await invokeWithRetry(this.config, this.http, "WorkflowRunsController_get", {
|
|
514
574
|
pathParams: { id: workflowId, runId },
|
|
@@ -519,7 +579,7 @@ var WorkflowRunsResource = class {
|
|
|
519
579
|
/**
|
|
520
580
|
* Start a workflow run and stream run events.
|
|
521
581
|
*
|
|
522
|
-
* @param workflowId -
|
|
582
|
+
* @param workflowId - Numeric workflow id to run.
|
|
523
583
|
* @param body - Run body (`message`, optional `sessionId`, optional `version` as `published` or `draft`).
|
|
524
584
|
* @param options.idempotencyKey - Client-supplied idempotency key; defaults to `body.runId` or a new uuid.
|
|
525
585
|
* @returns A stream of run events (tokens, completion, errors, and related signals).
|
|
@@ -587,18 +647,21 @@ var WorkflowRunsResource = class {
|
|
|
587
647
|
return this.createRun("ModusRunsController_create", body, {}, options);
|
|
588
648
|
}
|
|
589
649
|
/**
|
|
590
|
-
* Resume an
|
|
650
|
+
* Resume a run paused at an approval or connect interrupt, and stream the continuation's events.
|
|
591
651
|
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
652
|
+
* The decision is the whole turn: send no `message` (a non-empty one is rejected).
|
|
653
|
+
*
|
|
654
|
+
* @param runId - Id of the interrupted run.
|
|
655
|
+
* @param body - `sessionId` (the run's conversation id, required) and `decision`
|
|
656
|
+
* (`approve` / `deny` / `connected` / `cancelled`); optional `approvalTarget`.
|
|
594
657
|
* @param options.idempotencyKey - Client-supplied idempotency key; defaults to `body.runId` or a new uuid.
|
|
595
658
|
* @returns A stream of run events.
|
|
659
|
+
* @throws {ValidationError} When `sessionId` is blank, before any request is sent.
|
|
596
660
|
*
|
|
597
661
|
* @example
|
|
598
662
|
* ```ts
|
|
599
663
|
* const stream = client.workflows.runs.resume(runId, {
|
|
600
664
|
* decision: 'approve',
|
|
601
|
-
* message: 'Looks good — continue.',
|
|
602
665
|
* sessionId,
|
|
603
666
|
* })
|
|
604
667
|
* for await (const event of stream) {
|
|
@@ -608,6 +671,7 @@ var WorkflowRunsResource = class {
|
|
|
608
671
|
*/
|
|
609
672
|
resume(runId, body, options = {}) {
|
|
610
673
|
validateId(runId, "run_id");
|
|
674
|
+
requireSessionId(body.sessionId);
|
|
611
675
|
return this.createRun("ResumeRunsController_create", body, { runId }, options);
|
|
612
676
|
}
|
|
613
677
|
/**
|
|
@@ -628,39 +692,79 @@ var WorkflowRunsResource = class {
|
|
|
628
692
|
});
|
|
629
693
|
}
|
|
630
694
|
/**
|
|
631
|
-
*
|
|
695
|
+
* Poll a run's buffered events once (the JSON transport, not SSE).
|
|
632
696
|
*
|
|
633
697
|
* @param runId - Run id.
|
|
634
|
-
* @returns
|
|
698
|
+
* @returns The poll response: `events` (each with its stream `id` and `type`), `cursor`,
|
|
699
|
+
* `status` and `done`.
|
|
635
700
|
*/
|
|
636
701
|
async events(runId) {
|
|
637
702
|
validateId(runId, "run_id");
|
|
638
|
-
return invokeWithRetry(this.config, this.http, "RunLifecycleController_events", {
|
|
703
|
+
return await invokeWithRetry(this.config, this.http, "RunLifecycleController_events", {
|
|
639
704
|
pathParams: { runId }
|
|
640
705
|
});
|
|
641
706
|
}
|
|
642
707
|
/**
|
|
643
|
-
*
|
|
708
|
+
* Supersede a run's current turn with a follow-up turn.
|
|
709
|
+
*
|
|
710
|
+
* The run is cancelled and `nextRun` starts as a new run on the same conversation.
|
|
711
|
+
* On a run that has already finished nothing is cancelled, and `nextRun` simply
|
|
712
|
+
* starts a follow-up turn. `nextRun.runId` is pinned (a new uuid when you do not
|
|
713
|
+
* set one), so a retried request returns the same run instead of starting another.
|
|
644
714
|
*
|
|
645
715
|
* @param runId - Run id to interrupt.
|
|
716
|
+
* @param nextRun - The follow-up turn: `message` and `sessionId` (the run's conversation id).
|
|
717
|
+
* @param options.lastSeenEventId - Id of the last event you rendered, so the cancelled
|
|
718
|
+
* turn's saved output stops there.
|
|
719
|
+
* @returns The new run (`runId`, `status`); follow it with {@link stream}.
|
|
720
|
+
* @throws {ValidationError} When `nextRun.sessionId` is blank, before any request is sent.
|
|
721
|
+
*
|
|
722
|
+
* @example
|
|
723
|
+
* ```ts
|
|
724
|
+
* const { runId: nextRunId } = await client.workflows.runs.interrupt(runId, {
|
|
725
|
+
* message: 'Only count EMEA.',
|
|
726
|
+
* sessionId,
|
|
727
|
+
* })
|
|
728
|
+
* for await (const event of client.workflows.runs.stream(nextRunId)) {
|
|
729
|
+
* if (event.type === 'token') process.stdout.write(event.content)
|
|
730
|
+
* }
|
|
731
|
+
* ```
|
|
646
732
|
*/
|
|
647
|
-
async interrupt(runId) {
|
|
733
|
+
async interrupt(runId, nextRun, options = {}) {
|
|
648
734
|
validateId(runId, "run_id");
|
|
649
|
-
|
|
735
|
+
requireSessionId(nextRun.sessionId);
|
|
736
|
+
const jsonBody = {
|
|
737
|
+
nextRun: { ...nextRun, runId: nextRun.runId?.trim() || randomRunId() },
|
|
738
|
+
...options.lastSeenEventId ? { lastSeenEventId: options.lastSeenEventId } : {}
|
|
739
|
+
};
|
|
740
|
+
return await invokeWithRetry(this.config, this.http, "RunLifecycleController_interrupt", {
|
|
650
741
|
pathParams: { runId },
|
|
651
|
-
jsonBody
|
|
742
|
+
jsonBody
|
|
652
743
|
});
|
|
653
744
|
}
|
|
654
745
|
/**
|
|
655
|
-
*
|
|
746
|
+
* Replace the turn of a run that is still queued (not yet started).
|
|
656
747
|
*
|
|
657
748
|
* @param runId - Queued run id.
|
|
749
|
+
* @param nextRun - The replacement turn: `message` and `sessionId` (the run's conversation id).
|
|
750
|
+
* @returns The edited run (`runId`, `status`).
|
|
751
|
+
* @throws {ValidationError} When `nextRun.sessionId` is blank, before any request is sent.
|
|
752
|
+
* @throws {ConflictError} When the run has already started.
|
|
753
|
+
*
|
|
754
|
+
* @example
|
|
755
|
+
* ```ts
|
|
756
|
+
* await client.workflows.runs.editQueued(queuedRunId, {
|
|
757
|
+
* message: 'Summarize last week instead.',
|
|
758
|
+
* sessionId,
|
|
759
|
+
* })
|
|
760
|
+
* ```
|
|
658
761
|
*/
|
|
659
|
-
async editQueued(runId) {
|
|
762
|
+
async editQueued(runId, nextRun) {
|
|
660
763
|
validateId(runId, "run_id");
|
|
661
|
-
|
|
764
|
+
requireSessionId(nextRun.sessionId);
|
|
765
|
+
return await invokeWithRetry(this.config, this.http, "RunLifecycleController_editQueued", {
|
|
662
766
|
pathParams: { runId },
|
|
663
|
-
jsonBody: {}
|
|
767
|
+
jsonBody: { nextRun }
|
|
664
768
|
});
|
|
665
769
|
}
|
|
666
770
|
/**
|
|
@@ -688,12 +792,12 @@ var WorkflowRunsResource = class {
|
|
|
688
792
|
*
|
|
689
793
|
* @param sessionIds - Up to 100 session ids.
|
|
690
794
|
* @returns Active runs matching any of the given sessions.
|
|
691
|
-
* @throws {
|
|
795
|
+
* @throws {ValidationError} When more than 100 session ids are provided.
|
|
692
796
|
*/
|
|
693
797
|
async activeBySession(sessionIds) {
|
|
694
798
|
const uniqueSessionIds = [...new Set(sessionIds.map((id) => id.trim()).filter(Boolean))];
|
|
695
799
|
if (uniqueSessionIds.length > 100) {
|
|
696
|
-
throw new
|
|
800
|
+
throw new ValidationError("sessionIds must contain at most 100 ids");
|
|
697
801
|
}
|
|
698
802
|
return parseActiveRunsArray(
|
|
699
803
|
await invokeWithRetry(this.config, this.http, "RunLifecycleController_activeBySession", {
|
|
@@ -702,17 +806,19 @@ var WorkflowRunsResource = class {
|
|
|
702
806
|
);
|
|
703
807
|
}
|
|
704
808
|
/**
|
|
705
|
-
* Reconnect to
|
|
809
|
+
* Reconnect to a run and stream its events.
|
|
706
810
|
*
|
|
707
811
|
* @param runId - Run id to follow.
|
|
708
|
-
* @param options.lastEventId -
|
|
709
|
-
*
|
|
812
|
+
* @param options.lastEventId - The `id` of the last event you handled; the stream resumes
|
|
813
|
+
* after it. Omit to replay the run from its first event.
|
|
814
|
+
* @returns A stream of run events.
|
|
710
815
|
*
|
|
711
816
|
* @example
|
|
712
817
|
* ```ts
|
|
713
|
-
*
|
|
714
|
-
* for await (const event of stream) {
|
|
818
|
+
* let lastEventId: string | undefined
|
|
819
|
+
* for await (const event of client.workflows.runs.stream(runId, { lastEventId })) {
|
|
715
820
|
* if (event.type === 'token') process.stdout.write(event.content)
|
|
821
|
+
* lastEventId = event.id // after handling it: reconnect with it if the connection drops
|
|
716
822
|
* }
|
|
717
823
|
* ```
|
|
718
824
|
*/
|
|
@@ -803,17 +909,18 @@ var WorkflowsResource = class extends WorkflowsResourceBase {
|
|
|
803
909
|
this.workflowActions = new AgentWorkflowActionsResource(http, config);
|
|
804
910
|
}
|
|
805
911
|
/**
|
|
806
|
-
* Retrieve a workflow by
|
|
912
|
+
* Retrieve a workflow by its numeric id.
|
|
807
913
|
*
|
|
808
|
-
* @param workflowId - Numeric id
|
|
914
|
+
* @param workflowId - Numeric workflow id.
|
|
809
915
|
* @param options.view - Which saved version to return (`active` or `draft`).
|
|
810
916
|
* @param options.includeVariation - Include variation metadata when supported.
|
|
811
917
|
* @returns The workflow (`id`, `name`, `status`, …).
|
|
812
|
-
* @throws {NotFoundError} When no workflow
|
|
918
|
+
* @throws {NotFoundError} When no workflow has this id.
|
|
813
919
|
*
|
|
814
920
|
* @example
|
|
815
921
|
* ```ts
|
|
816
|
-
* const
|
|
922
|
+
* const [first] = (await client.workflows.list()).items
|
|
923
|
+
* const wf = await client.workflows.get(first.id)
|
|
817
924
|
* console.log(wf.name, wf.status)
|
|
818
925
|
* ```
|
|
819
926
|
*/
|
|
@@ -830,6 +937,29 @@ var WorkflowsResource = class extends WorkflowsResourceBase {
|
|
|
830
937
|
});
|
|
831
938
|
return parseWorkflow(data);
|
|
832
939
|
}
|
|
940
|
+
/**
|
|
941
|
+
* Chat with a published workflow. Node output streams live from the agent
|
|
942
|
+
* host: iterate `textStream()` for tokens, or `await` the result for the
|
|
943
|
+
* workflow's final output.
|
|
944
|
+
*
|
|
945
|
+
* @param workflowId - Numeric workflow id.
|
|
946
|
+
* @param message - User message text.
|
|
947
|
+
* @param options.model - Required model id (e.g. `"claude-sonnet-5.5"`).
|
|
948
|
+
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
949
|
+
* @returns A chat session — iterate it or `await` it for `content`, `threadId`, `runId`.
|
|
950
|
+
* @throws {ModusError} When no agent host is known for a custom `baseUrl`.
|
|
951
|
+
*
|
|
952
|
+
* @example
|
|
953
|
+
* ```ts
|
|
954
|
+
* const { content } = await client.workflows.chat(workflowId, 'Run this week\'s digest', {
|
|
955
|
+
* model: 'claude-sonnet-5.5',
|
|
956
|
+
* })
|
|
957
|
+
* console.log(content)
|
|
958
|
+
* ```
|
|
959
|
+
*/
|
|
960
|
+
chat(workflowId, message, options) {
|
|
961
|
+
return startChat(this.http, this.config, { kind: "workflows", id: workflowId }, message, options);
|
|
962
|
+
}
|
|
833
963
|
};
|
|
834
964
|
|
|
835
965
|
// src/_generated/resources/consumption/connections.ts
|
|
@@ -1084,7 +1214,7 @@ var ContextItemsResource = class {
|
|
|
1084
1214
|
* contextType: 'note',
|
|
1085
1215
|
* dataPath: ['finance', 'arr-definition'],
|
|
1086
1216
|
* })
|
|
1087
|
-
* if (found) console.log(found.uid, found.
|
|
1217
|
+
* if (found) console.log(found.uid, found.content)
|
|
1088
1218
|
* ```
|
|
1089
1219
|
*/
|
|
1090
1220
|
async lookup(options) {
|
|
@@ -1106,14 +1236,19 @@ var ContextItemsResource = class {
|
|
|
1106
1236
|
}
|
|
1107
1237
|
}
|
|
1108
1238
|
/**
|
|
1109
|
-
*
|
|
1239
|
+
* Page through the elements of an array field inside one context item's content.
|
|
1240
|
+
*
|
|
1241
|
+
* Each row is `{ index, value }`: the element's position in the array and the
|
|
1242
|
+
* element itself. A field that is not an array reads as one element; a missing
|
|
1243
|
+
* field as none. Rejects with a not-found error when the item does not exist,
|
|
1244
|
+
* you cannot read it, or its context type is not `contextType`.
|
|
1110
1245
|
*
|
|
1111
1246
|
* @param uid - Item uid.
|
|
1112
1247
|
* @param contextType - Context type of the item.
|
|
1113
|
-
* @param contentKeyPath - Dot path to the
|
|
1114
|
-
* @param options.pageSize - Maximum
|
|
1115
|
-
* @param options.pageToken - Opaque token from a previous page.
|
|
1116
|
-
* @returns A page of value rows.
|
|
1248
|
+
* @param contentKeyPath - Dot path to the array inside item content (e.g. `"enumValues"`).
|
|
1249
|
+
* @param options.pageSize - Maximum elements per page (default 25).
|
|
1250
|
+
* @param options.pageToken - Opaque token from a previous page of the same item.
|
|
1251
|
+
* @returns A page of `{ index, value }` rows.
|
|
1117
1252
|
*
|
|
1118
1253
|
* @example
|
|
1119
1254
|
* ```ts
|
|
@@ -1124,7 +1259,7 @@ var ContextItemsResource = class {
|
|
|
1124
1259
|
* 'enumValues',
|
|
1125
1260
|
* { pageSize: 25 },
|
|
1126
1261
|
* )
|
|
1127
|
-
* for (const row of page.items) console.log(row)
|
|
1262
|
+
* for (const row of page.items) console.log(row.index, row.value)
|
|
1128
1263
|
* ```
|
|
1129
1264
|
*/
|
|
1130
1265
|
listValues(uid, contextType, contentKeyPath, options = {}) {
|
|
@@ -1133,13 +1268,13 @@ var ContextItemsResource = class {
|
|
|
1133
1268
|
return this.listValuesPage(uid, contextType, contentKeyPath, pageSize, options.pageToken);
|
|
1134
1269
|
}
|
|
1135
1270
|
/**
|
|
1136
|
-
*
|
|
1271
|
+
* Page through an array field of a context item already loaded via {@link ContextItemsResource.list} or {@link ContextItemsResource.get}.
|
|
1137
1272
|
*
|
|
1138
1273
|
* @param item - Context item (uses `item.uid` and `item.contextType`).
|
|
1139
|
-
* @param contentKeyPath - Dot path to the
|
|
1140
|
-
* @param options.pageSize - Maximum
|
|
1141
|
-
* @param options.pageToken - Opaque token from a previous page.
|
|
1142
|
-
* @returns A page of value rows.
|
|
1274
|
+
* @param contentKeyPath - Dot path to the array inside item content.
|
|
1275
|
+
* @param options.pageSize - Maximum elements per page (default 25).
|
|
1276
|
+
* @param options.pageToken - Opaque token from a previous page of the same item.
|
|
1277
|
+
* @returns A page of `{ index, value }` rows.
|
|
1143
1278
|
*
|
|
1144
1279
|
* @example
|
|
1145
1280
|
* ```ts
|
|
@@ -1251,7 +1386,7 @@ var ModusConversationsResourceBase = class {
|
|
|
1251
1386
|
var ALLOWED_KINDS = /* @__PURE__ */ new Set(["all", "modus", "scopes"]);
|
|
1252
1387
|
function listParams(pageSize, pageToken, kind, source, sourceRef) {
|
|
1253
1388
|
if (kind !== void 0 && !ALLOWED_KINDS.has(kind)) {
|
|
1254
|
-
throw new
|
|
1389
|
+
throw new ValidationError(
|
|
1255
1390
|
`kind must be one of ${[...ALLOWED_KINDS].sort().join(", ")}, got ${JSON.stringify(kind)}`
|
|
1256
1391
|
);
|
|
1257
1392
|
}
|
|
@@ -1314,6 +1449,9 @@ var ModusResource = class {
|
|
|
1314
1449
|
/**
|
|
1315
1450
|
* Compose full-environment context for an intent without running chat.
|
|
1316
1451
|
*
|
|
1452
|
+
* Runs on the agent host, like `chat`, so a compose may take up to 90 seconds; keep `timeoutMs`
|
|
1453
|
+
* above that. A compose still running at 90 seconds fails with `UPSTREAM_TIMEOUT` (504).
|
|
1454
|
+
*
|
|
1317
1455
|
* @param message - Natural-language intent used to select context.
|
|
1318
1456
|
* @param options.limit - Maximum context items to return. Omit to return all selected items.
|
|
1319
1457
|
* @returns Composition metadata (counts / session id). Use `chat` for a reply.
|
|
@@ -1325,57 +1463,37 @@ var ModusResource = class {
|
|
|
1325
1463
|
* ```
|
|
1326
1464
|
*/
|
|
1327
1465
|
async getContext(message, options = {}) {
|
|
1328
|
-
const data = await invokeWithRetry(this.config, this.http, "
|
|
1466
|
+
const data = await invokeWithRetry(this.config, this.http, "ModusContextComposeController_compose", {
|
|
1329
1467
|
jsonBody: omitUndefined({ message, limit: options.limit })
|
|
1330
1468
|
});
|
|
1331
1469
|
return data;
|
|
1332
1470
|
}
|
|
1333
1471
|
/**
|
|
1334
|
-
*
|
|
1472
|
+
* Chat with Modus, the org-wide assistant. The reply streams live from the
|
|
1473
|
+
* agent host: iterate `textStream()` for tokens, or `await` the result for
|
|
1474
|
+
* the final answer. Pass the returned `threadId` back to continue.
|
|
1335
1475
|
*
|
|
1336
1476
|
* @param message - User message text.
|
|
1337
|
-
* @param options.model - Required model id (e.g. `"claude-sonnet-5"`).
|
|
1477
|
+
* @param options.model - Required model id (e.g. `"claude-sonnet-5.5"`).
|
|
1338
1478
|
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
1339
|
-
* @returns
|
|
1479
|
+
* @returns A chat session — iterate it or `await` it for `content`, `threadId`, `runId`.
|
|
1480
|
+
* @throws {ModusError} When no agent host is known for a custom `baseUrl`.
|
|
1340
1481
|
*
|
|
1341
1482
|
* @example
|
|
1342
1483
|
* ```ts
|
|
1343
1484
|
* const result = await client.modus.chat('What tables describe revenue?', {
|
|
1344
|
-
* model: 'claude-sonnet-5',
|
|
1485
|
+
* model: 'claude-sonnet-5.5',
|
|
1345
1486
|
* })
|
|
1346
|
-
* console.log(result.content
|
|
1347
|
-
* const follow =
|
|
1348
|
-
* model: 'claude-sonnet-5',
|
|
1487
|
+
* console.log(result.content)
|
|
1488
|
+
* const follow = client.modus.chat('Break that down by region.', {
|
|
1489
|
+
* model: 'claude-sonnet-5.5',
|
|
1349
1490
|
* threadId: result.threadId,
|
|
1350
1491
|
* })
|
|
1492
|
+
* for await (const token of follow.textStream()) process.stdout.write(token)
|
|
1351
1493
|
* ```
|
|
1352
1494
|
*/
|
|
1353
1495
|
chat(message, options) {
|
|
1354
|
-
return
|
|
1355
|
-
}
|
|
1356
|
-
/**
|
|
1357
|
-
* Stream a Modus reply token by token (SSE).
|
|
1358
|
-
*
|
|
1359
|
-
* The request starts when you consume `textStream()` or `eventStream()`.
|
|
1360
|
-
*
|
|
1361
|
-
* @param message - User message text.
|
|
1362
|
-
* @param options.model - Required model id (e.g. `"claude-sonnet-5"`).
|
|
1363
|
-
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
1364
|
-
* @returns Chat stream — iterate `textStream()` for tokens.
|
|
1365
|
-
*
|
|
1366
|
-
* @example
|
|
1367
|
-
* ```ts
|
|
1368
|
-
* const stream = client.modus.chatStream('Summarize ARR in three bullets.', {
|
|
1369
|
-
* model: 'claude-sonnet-5',
|
|
1370
|
-
* })
|
|
1371
|
-
* for await (const token of stream.textStream()) {
|
|
1372
|
-
* process.stdout.write(token)
|
|
1373
|
-
* }
|
|
1374
|
-
* console.log(stream.getFinalResult().threadId)
|
|
1375
|
-
* ```
|
|
1376
|
-
*/
|
|
1377
|
-
chatStream(message, options) {
|
|
1378
|
-
return modusChatStreamSession(this.http, this.config, message, options);
|
|
1496
|
+
return startChat(this.http, this.config, { kind: "modus" }, message, options);
|
|
1379
1497
|
}
|
|
1380
1498
|
};
|
|
1381
1499
|
|
|
@@ -1398,8 +1516,9 @@ var ScopesResourceBase = class {
|
|
|
1398
1516
|
* @param options.pageToken - Opaque page token from a previous response's `nextPageToken`. Omit
|
|
1399
1517
|
* for the first page.
|
|
1400
1518
|
* @param options.search - Case-insensitive substring filter on scope name.
|
|
1401
|
-
* @param options.view - Variation view. `active` reads deployed
|
|
1402
|
-
* snapshot.
|
|
1519
|
+
* @param options.view - Variation view. `active` reads the deployed version, or the latest draft
|
|
1520
|
+
* when nothing is published. `draft` reads the latest builder snapshot. `published` reads only
|
|
1521
|
+
* the published version, and only scopes you can use.
|
|
1403
1522
|
* @param options.managerId - Return only scopes supervised by this manager scope id.
|
|
1404
1523
|
* @returns A page of scopes.
|
|
1405
1524
|
*
|
|
@@ -1435,17 +1554,20 @@ var ScopesResourceBase = class {
|
|
|
1435
1554
|
/**
|
|
1436
1555
|
* Get a scope.
|
|
1437
1556
|
*
|
|
1438
|
-
* Returns a scope. `view=active` (default)
|
|
1439
|
-
*
|
|
1557
|
+
* Returns a scope. `view=active` (default) is the deployed version, or the latest draft when
|
|
1558
|
+
* nothing is published. `view=draft` is the latest edits. `view=published` is only the published
|
|
1559
|
+
* version, and only if you can use the scope. Returns 404 if you cannot access it.
|
|
1440
1560
|
*
|
|
1441
1561
|
* **Requires:** `scopes:read`
|
|
1442
1562
|
*
|
|
1443
1563
|
* @param scopeId - Numeric scope id.
|
|
1444
|
-
* @param options.view - Which
|
|
1564
|
+
* @param options.view - Which version to read. Defaults to `active`. `published` is only the
|
|
1565
|
+
* published version, and only if you can use the scope.
|
|
1445
1566
|
*
|
|
1446
1567
|
* @example
|
|
1447
1568
|
* ```ts
|
|
1448
|
-
* const
|
|
1569
|
+
* const [first] = (await client.scopes.list()).items
|
|
1570
|
+
* const scope = await client.scopes.get(first.id)
|
|
1449
1571
|
* console.log(scope.name, scope.status)
|
|
1450
1572
|
* ```
|
|
1451
1573
|
*/
|
|
@@ -1464,7 +1586,7 @@ var ScopesResource = class extends ScopesResourceBase {
|
|
|
1464
1586
|
/**
|
|
1465
1587
|
* Return the conversations sub-resource scoped to `scopeId`.
|
|
1466
1588
|
*
|
|
1467
|
-
* @param scopeId -
|
|
1589
|
+
* @param scopeId - Numeric scope id.
|
|
1468
1590
|
* @returns Conversations resource for the scope.
|
|
1469
1591
|
*
|
|
1470
1592
|
* @example
|
|
@@ -1483,9 +1605,11 @@ var ScopesResource = class extends ScopesResourceBase {
|
|
|
1483
1605
|
* Compose context this scope would use for an intent, without running chat.
|
|
1484
1606
|
*
|
|
1485
1607
|
* Large bodies are bounded inline previews. This REST method does not expose file references
|
|
1486
|
-
* or a full-content fetch operation.
|
|
1608
|
+
* or a full-content fetch operation. Runs on the agent host, like `chat`, so a compose may take
|
|
1609
|
+
* up to 90 seconds; keep `timeoutMs` above that. A compose still running at 90 seconds fails
|
|
1610
|
+
* with `UPSTREAM_TIMEOUT` (504).
|
|
1487
1611
|
*
|
|
1488
|
-
* @param scopeId -
|
|
1612
|
+
* @param scopeId - Numeric scope id.
|
|
1489
1613
|
* @param message - Natural-language intent used to select context.
|
|
1490
1614
|
* @param options.limit - Maximum context items to return. Omit to return all selected items.
|
|
1491
1615
|
* @returns Composed context text plus selection counts, item UIDs, and correlation session id.
|
|
@@ -1500,63 +1624,40 @@ var ScopesResource = class extends ScopesResourceBase {
|
|
|
1500
1624
|
*/
|
|
1501
1625
|
async getContext(scopeId, message, options = {}) {
|
|
1502
1626
|
validateId(scopeId, "scope_id");
|
|
1503
|
-
const data = await invokeWithRetry(this.config, this.http, "
|
|
1627
|
+
const data = await invokeWithRetry(this.config, this.http, "ScopeContextComposeController_compose", {
|
|
1504
1628
|
pathParams: { id: scopeId },
|
|
1505
1629
|
jsonBody: omitUndefined({ message, limit: options.limit })
|
|
1506
1630
|
});
|
|
1507
1631
|
return data;
|
|
1508
1632
|
}
|
|
1509
1633
|
/**
|
|
1510
|
-
*
|
|
1634
|
+
* Chat with a published scope. The reply streams live from the agent host:
|
|
1635
|
+
* iterate `textStream()` for tokens, or `await` the result for the final
|
|
1636
|
+
* answer. Pass the returned `threadId` back to continue the conversation.
|
|
1511
1637
|
*
|
|
1512
1638
|
* For the org-wide assistant use `client.modus.chat` instead.
|
|
1513
1639
|
*
|
|
1514
|
-
* @param scopeId -
|
|
1640
|
+
* @param scopeId - Numeric scope id (must be active for chat).
|
|
1515
1641
|
* @param message - User message text.
|
|
1516
|
-
* @param options.model - Required model id (e.g. `"claude-sonnet-5"`).
|
|
1642
|
+
* @param options.model - Required model id (e.g. `"claude-sonnet-5.5"`).
|
|
1517
1643
|
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
1518
|
-
* @returns
|
|
1644
|
+
* @returns A chat session — iterate it or `await` it for `content`, `threadId`, `runId`.
|
|
1645
|
+
* @throws {ModusError} When no agent host is known for a custom `baseUrl`.
|
|
1519
1646
|
*
|
|
1520
1647
|
* @example
|
|
1521
1648
|
* ```ts
|
|
1522
|
-
* const
|
|
1523
|
-
*
|
|
1524
|
-
* }
|
|
1525
|
-
* console.log(result.content, result.threadId)
|
|
1649
|
+
* const chat = client.scopes.chat(scopeId, 'What is our ARR trend?', { model: 'claude-sonnet-5.5' })
|
|
1650
|
+
* for await (const token of chat.textStream()) process.stdout.write(token)
|
|
1651
|
+
* const { threadId } = await chat
|
|
1526
1652
|
* const follow = await client.scopes.chat(scopeId, 'Break that down by segment.', {
|
|
1527
|
-
* model: 'claude-sonnet-5',
|
|
1528
|
-
* threadId
|
|
1653
|
+
* model: 'claude-sonnet-5.5',
|
|
1654
|
+
* threadId,
|
|
1529
1655
|
* })
|
|
1656
|
+
* console.log(follow.content)
|
|
1530
1657
|
* ```
|
|
1531
1658
|
*/
|
|
1532
1659
|
chat(scopeId, message, options) {
|
|
1533
|
-
return
|
|
1534
|
-
}
|
|
1535
|
-
/**
|
|
1536
|
-
* Stream a scope reply token by token (SSE).
|
|
1537
|
-
*
|
|
1538
|
-
* The request starts when you consume `textStream()` or `eventStream()`.
|
|
1539
|
-
*
|
|
1540
|
-
* @param scopeId - Scope numeric id or slug.
|
|
1541
|
-
* @param message - User message text.
|
|
1542
|
-
* @param options.model - Required model id (e.g. `"claude-sonnet-5"`).
|
|
1543
|
-
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
1544
|
-
* @param options.version - `"published"` or `"draft"`.
|
|
1545
|
-
* @returns Chat stream — iterate `textStream()` for tokens.
|
|
1546
|
-
*
|
|
1547
|
-
* @example
|
|
1548
|
-
* ```ts
|
|
1549
|
-
* const stream = client.scopes.chatStream(scopeId, 'Summarize in three bullets.', {
|
|
1550
|
-
* model: 'claude-sonnet-5',
|
|
1551
|
-
* })
|
|
1552
|
-
* for await (const token of stream.textStream()) {
|
|
1553
|
-
* process.stdout.write(token)
|
|
1554
|
-
* }
|
|
1555
|
-
* console.log(stream.getFinalResult().threadId)
|
|
1556
|
-
* ```
|
|
1557
|
-
*/
|
|
1558
|
-
chatStream(scopeId, message, options) {
|
|
1559
|
-
return chatStreamSession(this.http, this.config, "scopes", scopeId, message, options);
|
|
1660
|
+
return startChat(this.http, this.config, { kind: "scopes", id: scopeId }, message, options);
|
|
1560
1661
|
}
|
|
1561
1662
|
};
|
|
1562
1663
|
|
|
@@ -1586,12 +1687,15 @@ var SuggestionsResource = class {
|
|
|
1586
1687
|
* @param options.pageSize - Maximum items per page (default 5, max 12).
|
|
1587
1688
|
* @param options.pageToken - Opaque token from a previous page.
|
|
1588
1689
|
* @param options.scopeId - Filter to a single scope.
|
|
1589
|
-
* @param options.scopeIds - Filter to one or more scopes.
|
|
1690
|
+
* @param options.scopeIds - Filter to one or more scopes. An empty list throws; omit it for every scope.
|
|
1590
1691
|
* @returns A page of suggested questions.
|
|
1591
1692
|
*/
|
|
1592
1693
|
list(options = {}) {
|
|
1593
1694
|
const pageSize = options.pageSize ?? DEFAULT_SUGGESTIONS_PAGE_SIZE;
|
|
1594
1695
|
validatePageSize(pageSize, MAX_SUGGESTIONS_PAGE_SIZE);
|
|
1696
|
+
if (options.scopeIds?.length === 0) {
|
|
1697
|
+
throw new ValidationError("scopeIds requires at least one id; omit it to list suggestions for every scope");
|
|
1698
|
+
}
|
|
1595
1699
|
return this.listPage(pageSize, options.pageToken, options.scopeId, options.scopeIds);
|
|
1596
1700
|
}
|
|
1597
1701
|
async listPage(pageSize, pageToken, scopeId, scopeIds) {
|
|
@@ -1639,7 +1743,8 @@ export {
|
|
|
1639
1743
|
APIConnectionError,
|
|
1640
1744
|
AgentType,
|
|
1641
1745
|
AuthenticationError,
|
|
1642
|
-
|
|
1746
|
+
BadRequestError,
|
|
1747
|
+
ChatSession,
|
|
1643
1748
|
ConflictError,
|
|
1644
1749
|
InternalServerError,
|
|
1645
1750
|
Modus,
|