@tangle-network/agent-app 0.51.7 → 0.51.9

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.
@@ -27,3 +27,4 @@ export * from './resolve-attachments';
27
27
  export * from './attachment-upload';
28
28
  export * from './dispatch-parts';
29
29
  export * from './promote-file-part';
30
+ export * from './intelligence';
@@ -3000,6 +3000,106 @@ async function buildDispatchParts(input) {
3000
3000
  }
3001
3001
  return { succeeded: true, value: parts };
3002
3002
  }
3003
+
3004
+ // src/chat-routes/intelligence.ts
3005
+ function observeChatTurnStream(source, options) {
3006
+ const { client, sessionId, userId, workspaceId, runId, traceId, model, signal, onObservationError } = options;
3007
+ let state = "idle";
3008
+ let startedAt = 0;
3009
+ let interrupted = false;
3010
+ let observation;
3011
+ const finish = (termination) => {
3012
+ if (state !== "running") return observation ?? Promise.resolve();
3013
+ state = "terminal";
3014
+ const completedAt = Date.now();
3015
+ const observedTermination = signal?.aborted ? "interrupted" : termination;
3016
+ observation = Promise.resolve().then(async () => {
3017
+ await client().traceRun({
3018
+ ...runId !== void 0 ? { runId } : {},
3019
+ ...traceId !== void 0 ? { traceId } : {},
3020
+ ...model !== void 0 ? { model } : {},
3021
+ labels: {
3022
+ "tangle.sessionId": sessionId,
3023
+ ...userId !== void 0 ? { "tangle.userId": userId } : {},
3024
+ ...workspaceId !== void 0 ? { "tangle.workspaceId": workspaceId } : {},
3025
+ "tangle.observation.kind": "stream-lifecycle",
3026
+ "tangle.stream.termination": observedTermination,
3027
+ "tangle.started_at_ms": startedAt,
3028
+ "tangle.completed_at_ms": completedAt,
3029
+ "tangle.duration_ms": Math.max(0, completedAt - startedAt)
3030
+ }
3031
+ }, async () => void 0);
3032
+ }).catch(() => {
3033
+ try {
3034
+ onObservationError?.();
3035
+ } catch {
3036
+ }
3037
+ });
3038
+ return observation;
3039
+ };
3040
+ const invoke = async (method, operation) => {
3041
+ if (state === "idle") {
3042
+ if (method === "next") {
3043
+ state = "running";
3044
+ startedAt = Date.now();
3045
+ } else {
3046
+ state = "terminal";
3047
+ }
3048
+ }
3049
+ try {
3050
+ const result = await operation();
3051
+ if (method === "return" && state === "running") interrupted = true;
3052
+ if (result.done) await finish(interrupted ? "interrupted" : "exhausted");
3053
+ return result;
3054
+ } catch (error) {
3055
+ await finish("failed");
3056
+ throw error;
3057
+ }
3058
+ };
3059
+ return {
3060
+ [Symbol.asyncIterator]() {
3061
+ return this;
3062
+ },
3063
+ next(...args) {
3064
+ return invoke("next", () => source.next(...args));
3065
+ },
3066
+ return(value) {
3067
+ return invoke("return", () => source.return(value));
3068
+ },
3069
+ throw(error) {
3070
+ return invoke("throw", () => source.throw(error));
3071
+ },
3072
+ async [Symbol.asyncDispose]() {
3073
+ await this.return(void 0);
3074
+ }
3075
+ };
3076
+ }
3077
+ async function produceChatTurnWithIntelligence(options) {
3078
+ const snapshot = { ...options };
3079
+ const producer = await options.produce();
3080
+ return {
3081
+ stream: observeChatTurnStream(producer.stream, snapshot),
3082
+ finalText: () => producer.finalText(),
3083
+ get assistantParts() {
3084
+ return producer.assistantParts?.bind(producer);
3085
+ },
3086
+ get draftParts() {
3087
+ return producer.draftParts?.bind(producer);
3088
+ },
3089
+ get usage() {
3090
+ return producer.usage?.bind(producer);
3091
+ },
3092
+ get model() {
3093
+ return producer.model;
3094
+ },
3095
+ get modelFailover() {
3096
+ return producer.modelFailover?.bind(producer);
3097
+ },
3098
+ get modelAttribution() {
3099
+ return producer.modelAttribution?.bind(producer);
3100
+ }
3101
+ };
3102
+ }
3003
3103
  export {
3004
3104
  ALLOWED_ATTACHMENT_SNIFFED_MIMES,
3005
3105
  ATTACHMENT_ACCEPT,
@@ -3064,9 +3164,11 @@ export {
3064
3164
  mediaTypeForMentionPath,
3065
3165
  mentionKindForPath,
3066
3166
  normalizeChatPromptForSandbox,
3167
+ observeChatTurnStream,
3067
3168
  observeNativeCompletion,
3068
3169
  parseChatTurnParts,
3069
3170
  parseFileMentions,
3171
+ produceChatTurnWithIntelligence,
3070
3172
  promoteAgentFilePart,
3071
3173
  promptPartsByteSize,
3072
3174
  readCompletedSandboxTurn,