@kevin5251984/guild 0.2.12 → 0.2.14

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/src/oauth.ts CHANGED
@@ -1057,73 +1057,6 @@ function thinkingText(message: AssistantMessage | undefined): string {
1057
1057
  .trim();
1058
1058
  }
1059
1059
 
1060
- /**
1061
- * Codex `DEFAULT_STREAM_IDLE_TIMEOUT_MS`. No tokens on the stream — not a
1062
- * wall clock on the whole turn. Do not pass this as Pi `timeoutMs` for xAI:
1063
- * OpenAI-completions maps that to the SDK request timeout and kills thinking.
1064
- */
1065
- export const STREAM_IDLE_TIMEOUT_MS = 300_000;
1066
-
1067
- export class StreamIdleError extends Error {
1068
- readonly idleMs: number;
1069
- constructor(idleMs: number) {
1070
- super(
1071
- `stream idle: no tokens for ${Math.round(idleMs / 1000)}s (Codex-style; not a turn wall clock). Resend or switch models.`,
1072
- );
1073
- this.name = "StreamIdleError";
1074
- this.idleMs = idleMs;
1075
- }
1076
- }
1077
-
1078
- export function startStreamIdle(
1079
- idleMs: number,
1080
- parent?: AbortSignal,
1081
- ): {
1082
- signal: AbortSignal;
1083
- bump: () => void;
1084
- dispose: () => void;
1085
- timedOut: () => boolean;
1086
- } {
1087
- const ctrl = new AbortController();
1088
- let timer: ReturnType<typeof setTimeout> | undefined;
1089
- let timedOut = false;
1090
- const fire = () => {
1091
- if (timedOut || ctrl.signal.aborted) return;
1092
- timedOut = true;
1093
- ctrl.abort();
1094
- };
1095
- const bump = () => {
1096
- if (timedOut || parent?.aborted) return;
1097
- if (timer) clearTimeout(timer);
1098
- timer = setTimeout(fire, idleMs);
1099
- };
1100
- const onParent = () => {
1101
- if (timer) clearTimeout(timer);
1102
- ctrl.abort();
1103
- };
1104
- if (parent?.aborted) {
1105
- ctrl.abort();
1106
- } else {
1107
- parent?.addEventListener("abort", onParent, { once: true });
1108
- }
1109
- bump();
1110
- return {
1111
- signal: ctrl.signal,
1112
- bump,
1113
- dispose: () => {
1114
- if (timer) clearTimeout(timer);
1115
- parent?.removeEventListener("abort", onParent);
1116
- },
1117
- timedOut: () => timedOut,
1118
- };
1119
- }
1120
-
1121
- function userAborted(signal?: AbortSignal): Error {
1122
- const err = new Error("aborted");
1123
- err.name = "AbortError";
1124
- return err;
1125
- }
1126
-
1127
1060
  /** Pi streamSimple: Think chips only from thinking_delta, not a planted placeholder. */
1128
1061
  async function streamOAuthMessage(
1129
1062
  models: MutableModels,
@@ -1133,11 +1066,7 @@ async function streamOAuthMessage(
1133
1066
  toolCtx: ToolContext,
1134
1067
  traces: ToolTrace[],
1135
1068
  ): Promise<AssistantMessage> {
1136
- const idle = startStreamIdle(STREAM_IDLE_TIMEOUT_MS, options?.signal);
1137
- const stream = models.streamSimple(model, context, {
1138
- ...options,
1139
- signal: idle.signal,
1140
- });
1069
+ const stream = models.streamSimple(model, context, options);
1141
1070
  let lastEmit = 0;
1142
1071
  const flush = (partial: AssistantMessage | undefined, force: boolean) => {
1143
1072
  const think = thinkingText(partial);
@@ -1147,40 +1076,19 @@ async function streamOAuthMessage(
1147
1076
  lastEmit = now;
1148
1077
  emitProgress(toolCtx, traces, think);
1149
1078
  };
1150
- const failIfIdle = (): never => {
1151
- if (options?.signal?.aborted) throw userAborted(options.signal);
1152
- throw new StreamIdleError(STREAM_IDLE_TIMEOUT_MS);
1153
- };
1154
- try {
1155
- for await (const event of stream) {
1156
- idle.bump();
1157
- if (event.type === "thinking_start" || event.type === "thinking_delta") {
1158
- flush(event.partial, event.type === "thinking_start");
1159
- } else if (event.type === "thinking_end") {
1160
- flush(event.partial, true);
1161
- } else if (event.type === "done") {
1162
- flush(event.message, true);
1163
- return event.message;
1164
- } else if (event.type === "error") {
1165
- if (idle.timedOut() || options?.signal?.aborted) failIfIdle();
1166
- return event.error;
1167
- }
1168
- }
1169
- if (idle.timedOut() || options?.signal?.aborted) failIfIdle();
1170
- return stream.result();
1171
- } catch (err) {
1172
- if (err instanceof StreamIdleError) throw err;
1173
- if (err instanceof Error && err.name === "AbortError") {
1174
- if (options?.signal?.aborted) throw err;
1175
- if (idle.timedOut()) throw new StreamIdleError(STREAM_IDLE_TIMEOUT_MS);
1079
+ for await (const event of stream) {
1080
+ if (event.type === "thinking_start" || event.type === "thinking_delta") {
1081
+ flush(event.partial, event.type === "thinking_start");
1082
+ } else if (event.type === "thinking_end") {
1083
+ flush(event.partial, true);
1084
+ } else if (event.type === "done") {
1085
+ flush(event.message, true);
1086
+ return event.message;
1087
+ } else if (event.type === "error") {
1088
+ return event.error;
1176
1089
  }
1177
- if (idle.timedOut() && !(options?.signal?.aborted)) {
1178
- throw new StreamIdleError(STREAM_IDLE_TIMEOUT_MS);
1179
- }
1180
- throw err;
1181
- } finally {
1182
- idle.dispose();
1183
1090
  }
1091
+ return stream.result();
1184
1092
  }
1185
1093
 
1186
1094
  function abortWhen(signal?: AbortSignal): Promise<never> {
@@ -1344,33 +1252,21 @@ export async function completeOAuth(input: {
1344
1252
  if (fitted.length < transcript.length) {
1345
1253
  transcript.splice(0, transcript.length, ...fitted);
1346
1254
  }
1347
- let result: AssistantMessage;
1348
- try {
1349
- result = await Promise.race([
1350
- streamOAuthMessage(
1351
- models,
1352
- model,
1353
- {
1354
- systemPrompt: input.system,
1355
- messages: transcript,
1356
- ...(useTools ? { tools } : {}),
1357
- },
1358
- options,
1359
- toolCtx,
1360
- traces,
1361
- ),
1362
- abortWhen(options.signal),
1363
- ]);
1364
- } catch (err) {
1365
- if (err instanceof StreamIdleError) {
1366
- return {
1367
- calls: [],
1368
- text: err.message,
1369
- thinking: thinkingChunks.join("\n\n"),
1370
- };
1371
- }
1372
- throw err;
1373
- }
1255
+ const result = await Promise.race([
1256
+ streamOAuthMessage(
1257
+ models,
1258
+ model,
1259
+ {
1260
+ systemPrompt: input.system,
1261
+ messages: transcript,
1262
+ ...(useTools ? { tools } : {}),
1263
+ },
1264
+ options,
1265
+ toolCtx,
1266
+ traces,
1267
+ ),
1268
+ abortWhen(options.signal),
1269
+ ]);
1374
1270
  if (result.stopReason === "aborted" || toolCtx.signal?.aborted || options.signal?.aborted) {
1375
1271
  const err = new Error("aborted");
1376
1272
  err.name = "AbortError";