@getmodus/sdk 0.9.0 → 0.11.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 +166 -0
- package/README.md +189 -14
- package/dist/{conversations-D7ffhHIh.d.cts → _client-base-DdnxB9JA.d.cts} +4425 -1674
- package/dist/{conversations-D7ffhHIh.d.ts → _client-base-DdnxB9JA.d.ts} +4425 -1674
- package/dist/chunk-3MHHVC6C.cjs +2678 -0
- package/dist/chunk-3MHHVC6C.cjs.map +1 -0
- package/dist/{chunk-SP4UYRYG.js → chunk-JVTQBBMZ.js} +613 -304
- package/dist/chunk-JVTQBBMZ.js.map +1 -0
- package/dist/index.cjs +585 -2800
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +802 -614
- package/dist/index.d.ts +802 -614
- package/dist/index.js +434 -300
- 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-JVTQBBMZ.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.
|
|
651
|
+
*
|
|
652
|
+
* The decision is the whole turn: send no `message` (a non-empty one is rejected).
|
|
591
653
|
*
|
|
592
|
-
* @param runId -
|
|
593
|
-
* @param body -
|
|
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
|
|
@@ -950,6 +1080,40 @@ var ConnectionsResource = class extends ConnectionsResourceBase {
|
|
|
950
1080
|
}
|
|
951
1081
|
};
|
|
952
1082
|
|
|
1083
|
+
// src/_generated/resources/consumption/context/items.ts
|
|
1084
|
+
var ContextItemsResourceBase = class {
|
|
1085
|
+
/** @internal */
|
|
1086
|
+
constructor(http, config) {
|
|
1087
|
+
this.http = http;
|
|
1088
|
+
this.config = config;
|
|
1089
|
+
}
|
|
1090
|
+
/**
|
|
1091
|
+
* Query context items with SQL.
|
|
1092
|
+
*
|
|
1093
|
+
* Runs one read-only SQL statement over the `context_items` table and returns its rows. The query
|
|
1094
|
+
* sees only the items you can read. A query that is not read-only or not valid returns 400; one
|
|
1095
|
+
* that runs past 20 s returns 422.
|
|
1096
|
+
*
|
|
1097
|
+
* **Requires:** `context:read`
|
|
1098
|
+
*
|
|
1099
|
+
* @param query - Request body; see `ContextItemsQuery`.
|
|
1100
|
+
*
|
|
1101
|
+
* @example
|
|
1102
|
+
* ```ts
|
|
1103
|
+
* const result = await modus.context.items.query({
|
|
1104
|
+
* sql: 'SELECT context_type, count(*) AS items FROM context_items GROUP BY context_type',
|
|
1105
|
+
* })
|
|
1106
|
+
* result.rows
|
|
1107
|
+
* ```
|
|
1108
|
+
*/
|
|
1109
|
+
async query(query) {
|
|
1110
|
+
const data = await invokeWithRetry(this.config, this.http, "ContextItemsQueryController_query", {
|
|
1111
|
+
jsonBody: omitUndefined(query)
|
|
1112
|
+
});
|
|
1113
|
+
return data;
|
|
1114
|
+
}
|
|
1115
|
+
};
|
|
1116
|
+
|
|
953
1117
|
// src/resources/context/items.ts
|
|
954
1118
|
function contextListParams(pageSize, pageToken, filters = {}) {
|
|
955
1119
|
const extra = {};
|
|
@@ -976,12 +1140,7 @@ function parseLookupResponse(raw) {
|
|
|
976
1140
|
if (item === null || item === void 0) return void 0;
|
|
977
1141
|
return item;
|
|
978
1142
|
}
|
|
979
|
-
var ContextItemsResource = class {
|
|
980
|
-
/** @internal */
|
|
981
|
-
constructor(http, config) {
|
|
982
|
-
this.http = http;
|
|
983
|
-
this.config = config;
|
|
984
|
-
}
|
|
1143
|
+
var ContextItemsResource = class extends ContextItemsResourceBase {
|
|
985
1144
|
/**
|
|
986
1145
|
* List knowledge-base items.
|
|
987
1146
|
*
|
|
@@ -1084,7 +1243,7 @@ var ContextItemsResource = class {
|
|
|
1084
1243
|
* contextType: 'note',
|
|
1085
1244
|
* dataPath: ['finance', 'arr-definition'],
|
|
1086
1245
|
* })
|
|
1087
|
-
* if (found) console.log(found.uid, found.
|
|
1246
|
+
* if (found) console.log(found.uid, found.content)
|
|
1088
1247
|
* ```
|
|
1089
1248
|
*/
|
|
1090
1249
|
async lookup(options) {
|
|
@@ -1106,14 +1265,19 @@ var ContextItemsResource = class {
|
|
|
1106
1265
|
}
|
|
1107
1266
|
}
|
|
1108
1267
|
/**
|
|
1109
|
-
*
|
|
1268
|
+
* Page through the elements of an array field inside one context item's content.
|
|
1269
|
+
*
|
|
1270
|
+
* Each row is `{ index, value }`: the element's position in the array and the
|
|
1271
|
+
* element itself. A field that is not an array reads as one element; a missing
|
|
1272
|
+
* field as none. Rejects with a not-found error when the item does not exist,
|
|
1273
|
+
* you cannot read it, or its context type is not `contextType`.
|
|
1110
1274
|
*
|
|
1111
1275
|
* @param uid - Item uid.
|
|
1112
1276
|
* @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.
|
|
1277
|
+
* @param contentKeyPath - Dot path to the array inside item content (e.g. `"enumValues"`).
|
|
1278
|
+
* @param options.pageSize - Maximum elements per page (default 25).
|
|
1279
|
+
* @param options.pageToken - Opaque token from a previous page of the same item.
|
|
1280
|
+
* @returns A page of `{ index, value }` rows.
|
|
1117
1281
|
*
|
|
1118
1282
|
* @example
|
|
1119
1283
|
* ```ts
|
|
@@ -1124,7 +1288,7 @@ var ContextItemsResource = class {
|
|
|
1124
1288
|
* 'enumValues',
|
|
1125
1289
|
* { pageSize: 25 },
|
|
1126
1290
|
* )
|
|
1127
|
-
* for (const row of page.items) console.log(row)
|
|
1291
|
+
* for (const row of page.items) console.log(row.index, row.value)
|
|
1128
1292
|
* ```
|
|
1129
1293
|
*/
|
|
1130
1294
|
listValues(uid, contextType, contentKeyPath, options = {}) {
|
|
@@ -1133,13 +1297,13 @@ var ContextItemsResource = class {
|
|
|
1133
1297
|
return this.listValuesPage(uid, contextType, contentKeyPath, pageSize, options.pageToken);
|
|
1134
1298
|
}
|
|
1135
1299
|
/**
|
|
1136
|
-
*
|
|
1300
|
+
* Page through an array field of a context item already loaded via {@link ContextItemsResource.list} or {@link ContextItemsResource.get}.
|
|
1137
1301
|
*
|
|
1138
1302
|
* @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.
|
|
1303
|
+
* @param contentKeyPath - Dot path to the array inside item content.
|
|
1304
|
+
* @param options.pageSize - Maximum elements per page (default 25).
|
|
1305
|
+
* @param options.pageToken - Opaque token from a previous page of the same item.
|
|
1306
|
+
* @returns A page of `{ index, value }` rows.
|
|
1143
1307
|
*
|
|
1144
1308
|
* @example
|
|
1145
1309
|
* ```ts
|
|
@@ -1251,7 +1415,7 @@ var ModusConversationsResourceBase = class {
|
|
|
1251
1415
|
var ALLOWED_KINDS = /* @__PURE__ */ new Set(["all", "modus", "scopes"]);
|
|
1252
1416
|
function listParams(pageSize, pageToken, kind, source, sourceRef) {
|
|
1253
1417
|
if (kind !== void 0 && !ALLOWED_KINDS.has(kind)) {
|
|
1254
|
-
throw new
|
|
1418
|
+
throw new ValidationError(
|
|
1255
1419
|
`kind must be one of ${[...ALLOWED_KINDS].sort().join(", ")}, got ${JSON.stringify(kind)}`
|
|
1256
1420
|
);
|
|
1257
1421
|
}
|
|
@@ -1314,6 +1478,9 @@ var ModusResource = class {
|
|
|
1314
1478
|
/**
|
|
1315
1479
|
* Compose full-environment context for an intent without running chat.
|
|
1316
1480
|
*
|
|
1481
|
+
* Runs on the agent host, like `chat`, so a compose may take up to 90 seconds; keep `timeoutMs`
|
|
1482
|
+
* above that. A compose still running at 90 seconds fails with `UPSTREAM_TIMEOUT` (504).
|
|
1483
|
+
*
|
|
1317
1484
|
* @param message - Natural-language intent used to select context.
|
|
1318
1485
|
* @param options.limit - Maximum context items to return. Omit to return all selected items.
|
|
1319
1486
|
* @returns Composition metadata (counts / session id). Use `chat` for a reply.
|
|
@@ -1325,57 +1492,37 @@ var ModusResource = class {
|
|
|
1325
1492
|
* ```
|
|
1326
1493
|
*/
|
|
1327
1494
|
async getContext(message, options = {}) {
|
|
1328
|
-
const data = await invokeWithRetry(this.config, this.http, "
|
|
1495
|
+
const data = await invokeWithRetry(this.config, this.http, "ModusContextComposeController_compose", {
|
|
1329
1496
|
jsonBody: omitUndefined({ message, limit: options.limit })
|
|
1330
1497
|
});
|
|
1331
1498
|
return data;
|
|
1332
1499
|
}
|
|
1333
1500
|
/**
|
|
1334
|
-
*
|
|
1501
|
+
* Chat with Modus, the org-wide assistant. The reply streams live from the
|
|
1502
|
+
* agent host: iterate `textStream()` for tokens, or `await` the result for
|
|
1503
|
+
* the final answer. Pass the returned `threadId` back to continue.
|
|
1335
1504
|
*
|
|
1336
1505
|
* @param message - User message text.
|
|
1337
|
-
* @param options.model - Required model id (e.g. `"claude-sonnet-5"`).
|
|
1506
|
+
* @param options.model - Required model id (e.g. `"claude-sonnet-5.5"`).
|
|
1338
1507
|
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
1339
|
-
* @returns
|
|
1508
|
+
* @returns A chat session — iterate it or `await` it for `content`, `threadId`, `runId`.
|
|
1509
|
+
* @throws {ModusError} When no agent host is known for a custom `baseUrl`.
|
|
1340
1510
|
*
|
|
1341
1511
|
* @example
|
|
1342
1512
|
* ```ts
|
|
1343
1513
|
* const result = await client.modus.chat('What tables describe revenue?', {
|
|
1344
|
-
* model: 'claude-sonnet-5',
|
|
1514
|
+
* model: 'claude-sonnet-5.5',
|
|
1345
1515
|
* })
|
|
1346
|
-
* console.log(result.content
|
|
1347
|
-
* const follow =
|
|
1348
|
-
* model: 'claude-sonnet-5',
|
|
1516
|
+
* console.log(result.content)
|
|
1517
|
+
* const follow = client.modus.chat('Break that down by region.', {
|
|
1518
|
+
* model: 'claude-sonnet-5.5',
|
|
1349
1519
|
* threadId: result.threadId,
|
|
1350
1520
|
* })
|
|
1521
|
+
* for await (const token of follow.textStream()) process.stdout.write(token)
|
|
1351
1522
|
* ```
|
|
1352
1523
|
*/
|
|
1353
1524
|
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);
|
|
1525
|
+
return startChat(this.http, this.config, { kind: "modus" }, message, options);
|
|
1379
1526
|
}
|
|
1380
1527
|
};
|
|
1381
1528
|
|
|
@@ -1398,8 +1545,9 @@ var ScopesResourceBase = class {
|
|
|
1398
1545
|
* @param options.pageToken - Opaque page token from a previous response's `nextPageToken`. Omit
|
|
1399
1546
|
* for the first page.
|
|
1400
1547
|
* @param options.search - Case-insensitive substring filter on scope name.
|
|
1401
|
-
* @param options.view - Variation view. `active` reads deployed
|
|
1402
|
-
* snapshot.
|
|
1548
|
+
* @param options.view - Variation view. `active` reads the deployed version, or the latest draft
|
|
1549
|
+
* when nothing is published. `draft` reads the latest builder snapshot. `published` reads only
|
|
1550
|
+
* the published version, and only scopes you can use.
|
|
1403
1551
|
* @param options.managerId - Return only scopes supervised by this manager scope id.
|
|
1404
1552
|
* @returns A page of scopes.
|
|
1405
1553
|
*
|
|
@@ -1435,17 +1583,20 @@ var ScopesResourceBase = class {
|
|
|
1435
1583
|
/**
|
|
1436
1584
|
* Get a scope.
|
|
1437
1585
|
*
|
|
1438
|
-
* Returns a scope. `view=active` (default)
|
|
1439
|
-
*
|
|
1586
|
+
* Returns a scope. `view=active` (default) is the deployed version, or the latest draft when
|
|
1587
|
+
* nothing is published. `view=draft` is the latest edits. `view=published` is only the published
|
|
1588
|
+
* version, and only if you can use the scope. Returns 404 if you cannot access it.
|
|
1440
1589
|
*
|
|
1441
1590
|
* **Requires:** `scopes:read`
|
|
1442
1591
|
*
|
|
1443
1592
|
* @param scopeId - Numeric scope id.
|
|
1444
|
-
* @param options.view - Which
|
|
1593
|
+
* @param options.view - Which version to read. Defaults to `active`. `published` is only the
|
|
1594
|
+
* published version, and only if you can use the scope.
|
|
1445
1595
|
*
|
|
1446
1596
|
* @example
|
|
1447
1597
|
* ```ts
|
|
1448
|
-
* const
|
|
1598
|
+
* const [first] = (await client.scopes.list()).items
|
|
1599
|
+
* const scope = await client.scopes.get(first.id)
|
|
1449
1600
|
* console.log(scope.name, scope.status)
|
|
1450
1601
|
* ```
|
|
1451
1602
|
*/
|
|
@@ -1464,7 +1615,7 @@ var ScopesResource = class extends ScopesResourceBase {
|
|
|
1464
1615
|
/**
|
|
1465
1616
|
* Return the conversations sub-resource scoped to `scopeId`.
|
|
1466
1617
|
*
|
|
1467
|
-
* @param scopeId -
|
|
1618
|
+
* @param scopeId - Numeric scope id.
|
|
1468
1619
|
* @returns Conversations resource for the scope.
|
|
1469
1620
|
*
|
|
1470
1621
|
* @example
|
|
@@ -1483,9 +1634,11 @@ var ScopesResource = class extends ScopesResourceBase {
|
|
|
1483
1634
|
* Compose context this scope would use for an intent, without running chat.
|
|
1484
1635
|
*
|
|
1485
1636
|
* Large bodies are bounded inline previews. This REST method does not expose file references
|
|
1486
|
-
* or a full-content fetch operation.
|
|
1637
|
+
* or a full-content fetch operation. Runs on the agent host, like `chat`, so a compose may take
|
|
1638
|
+
* up to 90 seconds; keep `timeoutMs` above that. A compose still running at 90 seconds fails
|
|
1639
|
+
* with `UPSTREAM_TIMEOUT` (504).
|
|
1487
1640
|
*
|
|
1488
|
-
* @param scopeId -
|
|
1641
|
+
* @param scopeId - Numeric scope id.
|
|
1489
1642
|
* @param message - Natural-language intent used to select context.
|
|
1490
1643
|
* @param options.limit - Maximum context items to return. Omit to return all selected items.
|
|
1491
1644
|
* @returns Composed context text plus selection counts, item UIDs, and correlation session id.
|
|
@@ -1500,63 +1653,40 @@ var ScopesResource = class extends ScopesResourceBase {
|
|
|
1500
1653
|
*/
|
|
1501
1654
|
async getContext(scopeId, message, options = {}) {
|
|
1502
1655
|
validateId(scopeId, "scope_id");
|
|
1503
|
-
const data = await invokeWithRetry(this.config, this.http, "
|
|
1656
|
+
const data = await invokeWithRetry(this.config, this.http, "ScopeContextComposeController_compose", {
|
|
1504
1657
|
pathParams: { id: scopeId },
|
|
1505
1658
|
jsonBody: omitUndefined({ message, limit: options.limit })
|
|
1506
1659
|
});
|
|
1507
1660
|
return data;
|
|
1508
1661
|
}
|
|
1509
1662
|
/**
|
|
1510
|
-
*
|
|
1663
|
+
* Chat with a published scope. The reply streams live from the agent host:
|
|
1664
|
+
* iterate `textStream()` for tokens, or `await` the result for the final
|
|
1665
|
+
* answer. Pass the returned `threadId` back to continue the conversation.
|
|
1511
1666
|
*
|
|
1512
1667
|
* For the org-wide assistant use `client.modus.chat` instead.
|
|
1513
1668
|
*
|
|
1514
|
-
* @param scopeId -
|
|
1669
|
+
* @param scopeId - Numeric scope id (must be active for chat).
|
|
1515
1670
|
* @param message - User message text.
|
|
1516
|
-
* @param options.model - Required model id (e.g. `"claude-sonnet-5"`).
|
|
1671
|
+
* @param options.model - Required model id (e.g. `"claude-sonnet-5.5"`).
|
|
1517
1672
|
* @param options.threadId - Continue an existing conversation; omit to start new.
|
|
1518
|
-
* @returns
|
|
1673
|
+
* @returns A chat session — iterate it or `await` it for `content`, `threadId`, `runId`.
|
|
1674
|
+
* @throws {ModusError} When no agent host is known for a custom `baseUrl`.
|
|
1519
1675
|
*
|
|
1520
1676
|
* @example
|
|
1521
1677
|
* ```ts
|
|
1522
|
-
* const
|
|
1523
|
-
*
|
|
1524
|
-
* }
|
|
1525
|
-
* console.log(result.content, result.threadId)
|
|
1678
|
+
* const chat = client.scopes.chat(scopeId, 'What is our ARR trend?', { model: 'claude-sonnet-5.5' })
|
|
1679
|
+
* for await (const token of chat.textStream()) process.stdout.write(token)
|
|
1680
|
+
* const { threadId } = await chat
|
|
1526
1681
|
* const follow = await client.scopes.chat(scopeId, 'Break that down by segment.', {
|
|
1527
|
-
* model: 'claude-sonnet-5',
|
|
1528
|
-
* threadId
|
|
1682
|
+
* model: 'claude-sonnet-5.5',
|
|
1683
|
+
* threadId,
|
|
1529
1684
|
* })
|
|
1685
|
+
* console.log(follow.content)
|
|
1530
1686
|
* ```
|
|
1531
1687
|
*/
|
|
1532
1688
|
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);
|
|
1689
|
+
return startChat(this.http, this.config, { kind: "scopes", id: scopeId }, message, options);
|
|
1560
1690
|
}
|
|
1561
1691
|
};
|
|
1562
1692
|
|
|
@@ -1586,12 +1716,15 @@ var SuggestionsResource = class {
|
|
|
1586
1716
|
* @param options.pageSize - Maximum items per page (default 5, max 12).
|
|
1587
1717
|
* @param options.pageToken - Opaque token from a previous page.
|
|
1588
1718
|
* @param options.scopeId - Filter to a single scope.
|
|
1589
|
-
* @param options.scopeIds - Filter to one or more scopes.
|
|
1719
|
+
* @param options.scopeIds - Filter to one or more scopes. An empty list throws; omit it for every scope.
|
|
1590
1720
|
* @returns A page of suggested questions.
|
|
1591
1721
|
*/
|
|
1592
1722
|
list(options = {}) {
|
|
1593
1723
|
const pageSize = options.pageSize ?? DEFAULT_SUGGESTIONS_PAGE_SIZE;
|
|
1594
1724
|
validatePageSize(pageSize, MAX_SUGGESTIONS_PAGE_SIZE);
|
|
1725
|
+
if (options.scopeIds?.length === 0) {
|
|
1726
|
+
throw new ValidationError("scopeIds requires at least one id; omit it to list suggestions for every scope");
|
|
1727
|
+
}
|
|
1595
1728
|
return this.listPage(pageSize, options.pageToken, options.scopeId, options.scopeIds);
|
|
1596
1729
|
}
|
|
1597
1730
|
async listPage(pageSize, pageToken, scopeId, scopeIds) {
|
|
@@ -1639,7 +1772,8 @@ export {
|
|
|
1639
1772
|
APIConnectionError,
|
|
1640
1773
|
AgentType,
|
|
1641
1774
|
AuthenticationError,
|
|
1642
|
-
|
|
1775
|
+
BadRequestError,
|
|
1776
|
+
ChatSession,
|
|
1643
1777
|
ConflictError,
|
|
1644
1778
|
InternalServerError,
|
|
1645
1779
|
Modus,
|