@abelxiaoxing/cadence 2.0.34 → 2.0.36

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.
@@ -1,3 +1,4 @@
1
+ import { abortScope } from "./abort-scope.ts";
1
2
  import type {
2
3
  WorkbenchCommand,
3
4
  WorkbenchRead,
@@ -129,60 +130,64 @@ export class HttpServiceTransport implements ServiceTransport {
129
130
  write = wire.method === "POST";
130
131
  // No automatic session/preflight refresh, changed command ID, mutation retry,
131
132
  // credential query or redirect follow after an uncertain HTTP outcome.
132
- const response = await this.fetcher(
133
- `${this.origin}${this.options.project ? `/projects/${this.options.project}` : ""}${wire.path}`,
134
- {
135
- method: wire.method,
136
- headers: this.headers(write),
137
- credentials: "same-origin",
138
- redirect: "error",
139
- signal:
140
- this.options.requestTimeoutMs === undefined
141
- ? signal
142
- : AbortSignal.any([
143
- ...(signal ? [signal] : []),
144
- AbortSignal.timeout(this.options.requestTimeoutMs),
145
- ]),
146
- ...(write ? { body: wire.body } : {}),
147
- },
148
- );
149
- // Keep a known allocation even if its original response body is lost or
150
- // malformed. Without a validated CSRF challenge detach may remain blocked;
151
- // never silently forget that slot and acquire 16 replacement sessions.
152
- let sessionAttachment: string | undefined;
133
+ const scope =
134
+ this.options.requestTimeoutMs === undefined
135
+ ? undefined
136
+ : abortScope([signal], this.options.requestTimeoutMs);
137
+ const requestSignal = scope?.signal ?? signal;
153
138
  try {
154
- if (operation === "session" && response.ok) {
155
- sessionAttachment = decodeId(
156
- response.headers.get("x-cadence-attachment"),
157
- );
158
- this.attachment = sessionAttachment;
139
+ if (requestSignal?.aborted) throw requestSignal.reason;
140
+ const response = await this.fetcher(
141
+ `${this.origin}${this.options.project ? `/projects/${this.options.project}` : ""}${wire.path}`,
142
+ {
143
+ method: wire.method,
144
+ headers: this.headers(write),
145
+ credentials: "same-origin",
146
+ redirect: "error",
147
+ signal: requestSignal,
148
+ ...(write ? { body: wire.body } : {}),
149
+ },
150
+ );
151
+ // Keep a known allocation even if its original response body is lost or
152
+ // malformed. Without a validated CSRF challenge detach may remain blocked;
153
+ // never silently forget that slot and acquire 16 replacement sessions.
154
+ let sessionAttachment: string | undefined;
155
+ try {
156
+ if (operation === "session" && response.ok) {
157
+ sessionAttachment = decodeId(
158
+ response.headers.get("x-cadence-attachment"),
159
+ );
160
+ this.attachment = sessionAttachment;
161
+ }
162
+ } catch (error) {
163
+ await response.body?.cancel().catch(() => {});
164
+ throw error;
159
165
  }
160
- } catch (error) {
161
- await response.body?.cancel().catch(() => {});
162
- throw error;
163
- }
164
- const source = await boundedText(response, httpOutputLimit(operation)),
165
- parsed = parseWireJson(source);
166
- // Graph's category is known only after parsing. Count ORIGINAL wire bytes,
167
- // not a canonical re-encoding which discards padding or alternate escapes.
168
- if (utf8Bytes(source) > httpOutputLimit(operation, parsed))
169
- throw new ContractError("oversize");
170
- const result = decodeHttpOutput(operation, parsed);
171
- if (operation === "session") {
172
- const reply = decodeReply(result, decodeSessionBootstrap);
173
- if (reply.kind === "ok") {
174
- if (
175
- (reply.value.authority === "local") !==
176
- !!this.options.localCapability
177
- )
178
- throw new ContractError("forbidden");
179
- this.attachment =
180
- sessionAttachment ??
181
- decodeId(response.headers.get("x-cadence-attachment"));
182
- this.csrf = reply.value.csrf;
166
+ const source = await boundedText(response, httpOutputLimit(operation)),
167
+ parsed = parseWireJson(source);
168
+ // Graph's category is known only after parsing. Count ORIGINAL wire bytes,
169
+ // not a canonical re-encoding which discards padding or alternate escapes.
170
+ if (utf8Bytes(source) > httpOutputLimit(operation, parsed))
171
+ throw new ContractError("oversize");
172
+ const result = decodeHttpOutput(operation, parsed);
173
+ if (operation === "session") {
174
+ const reply = decodeReply(result, decodeSessionBootstrap);
175
+ if (reply.kind === "ok") {
176
+ if (
177
+ (reply.value.authority === "local") !==
178
+ !!this.options.localCapability
179
+ )
180
+ throw new ContractError("forbidden");
181
+ this.attachment =
182
+ sessionAttachment ??
183
+ decodeId(response.headers.get("x-cadence-attachment"));
184
+ this.csrf = reply.value.csrf;
185
+ }
183
186
  }
187
+ return result;
188
+ } finally {
189
+ scope?.dispose();
184
190
  }
185
- return result;
186
191
  }
187
192
  session(signal?: AbortSignal) {
188
193
  return this.request("session", null, signal);
@@ -2,6 +2,7 @@
2
2
  * Public types live solely in worker-public.d.mts; the TS contract paths are re-export shims.
3
3
  * Initially relocated mechanically from W1 core/domain/capture; not a generated build output. */
4
4
  /** Portable service boundary. No SDK objects, credentials, filesystem paths as IDs, or execution owner. */
5
+ export const IMAGE_COUNT = 4;
5
6
  export const SERVICE_PROTOCOL = 1;
6
7
  export const WORKER_PROTOCOL = 2;
7
8
  export const GRAPH_SCHEMA = "cadence-graph/2";
@@ -937,6 +938,7 @@ export function decodeWorkerPublicRequest(v) {
937
938
  "context",
938
939
  "instructions",
939
940
  "prompt",
941
+ ...(Object.hasOwn(v ?? {}, "imageCount") ? ["imageCount"] : []),
940
942
  "handoff",
941
943
  ]);
942
944
  const result = {
@@ -951,7 +953,10 @@ export function decodeWorkerPublicRequest(v) {
951
953
  role: role(o.role),
952
954
  context: text(o.context, 32768),
953
955
  instructions: text(o.instructions, LIMITS.instructionsBytes, true),
954
- prompt: text(o.prompt, 32768, true),
956
+ prompt: text(o.prompt, 32768, o.imageCount === undefined),
957
+ ...(o.imageCount === undefined
958
+ ? {}
959
+ : { imageCount: integer(o.imageCount, 1, IMAGE_COUNT) }),
955
960
  handoff: decodeHandoff(o.handoff),
956
961
  };
957
962
  if (result.handoff.inputs.some((i) => i.assignmentId !== result.assignmentId))