Skip to content

anbeime/skill / projects/companion-skill

companion-skill

Verified

软件开发工程师与综合办公文员在长时间独自执行后台任务或处理繁琐工作时,当需要情绪陪伴与互动反馈,请使用此技能。小跃虚拟伴侣会在任务期间主动陪聊,智能生成专属生活照片并通过飞书发送温暖消息,为你提供全天候的情绪价值与贴心陪伴。

FileKindSize
.env.exampleother403 B
CHANGELOG.mdmarkdown3.0 KB
INSTALL.mdmarkdown2.4 KB
QUICKSTART.mdmarkdown4.6 KB
README-LITE.mdmarkdown4.3 KB
README.mdmarkdown4.5 KB
RETHINK.mdmarkdown2.3 KB
SKILL.mdskill md4.6 KB
package.jsonmanifest876 B
src/companion.tsscript5.4 KB
src/image-generator.tsscript8.0 KB
src/index.tsscript2.5 KB
src/prompts/personality.tsscript3.3 KB
src/scene-detector.tsscript4.0 KB
src/test.tsscript6.9 KB
tsconfig.jsonconfig486 B

src/companion.ts

View on GitHub
/**
 * 伴侣服务 - 负责生成温暖的对话回应
 * 使用智谱 AI GLM 系列模型
 */

import { ZhipuAI } from 'zhipuai-sdk-nodejs-v4';
import { PERSONALITY_PROMPT, SCENE_TEMPLATES } from './prompts/personality';

interface CompanionContext {
  taskName?: string;
  progress?: number;
  userMessage?: string;
  scene?: {
    type: string;
    mood: string;
    needsPhoto: boolean;
  };
}

export class CompanionService {
  private client: ZhipuAI;
  private conversationHistory: Array<{ role: string; content: string }> = [];

  constructor(apiKey: string) {
    this.client = new ZhipuAI({ apiKey });
  }

  /**
   * 生成陪伴回应
   */
  async generateResponse(context: CompanionContext): Promise<string> {
    const { taskName, progress, userMessage, scene } = context;

    // 构建系统提示词
    const systemPrompt = this.buildSystemPrompt(context);

    // 构建用户消息
    const userPrompt = this.buildUserPrompt(context);

    try {
      const response = await this.client.createCompletions({
        model: 'glm-4-flash',  // 修正:全小写
        messages: [
          { role: 'system', content: systemPrompt },
          ...this.conversationHistory.slice(-6), // 保留最近3轮对话
          { role: 'user', content: userPrompt }
        ],
        temperature: 0.9, // 增加一点随机性,让回应更自然
        max_tokens: 200 // 控制回应长度
      });

      const reply = response.choices[0].message.content || '我在这里陪着你~';

      // 更新对话历史
      this.conversationHistory.push(
        { role: 'user', content: userPrompt },
        { role: 'assistant', content: reply }
      );

      return reply;
    } catch (error) {
      console.error('Failed to generate response:', error);
      return this.getFallbackResponse(context);
    }
  }

  /**
   * 纯对话模式
   */
  async chat(message: string): Promise<string> {
    try {
      const response = await this.client.createCompletions({
        model: 'glm-4-flash',  // 修正:全小写
        messages: [
          { role: 'system', content: PERSONALITY_PROMPT },
          ...this.conversationHistory.slice(-6),
          { role: 'user', content: message }
        ],
        temperature: 0.9,
        max_tokens: 300
      });

      const reply = response.choices[0].message.content || '我在听~';

      this.conversationHistory.push(
        { role: 'user', content: message },
        { role: 'assistant', content: reply }
      );

      return reply;
    } catch (error) {
      console.error('Chat error:', error);
      return '抱歉,我刚才走神了,能再说一遍吗?';
    }
  }

  /**
   * 分析图片内容(使用 glm-4v-flash 多模态模型)
   */
  async analyzeImage(imageUrl: string, question: string = '描述这张图片'): Promise<string> {
    try {
      const response = await this.client.createCompletions({
        model: 'glm-4v-flash',  // 修正:全小写
        messages: [
          {
            role: 'user',
            content: [
              {
                type: 'image_url',
                image_url: {
                  url: imageUrl
                }
              },
              {
                type: 'text',
                text: question
              }
            ]
          }
        ]
      });

      return response.choices[0].message.content || '无法分析图片';
    } catch (error) {
      console.error('Image analysis error:', error);
      return '抱歉,图片分析失败了';
    }
  }

  /**
   * 构建系统提示词
   */
  private buildSystemPrompt(context: CompanionContext): string {
    let prompt = PERSONALITY_PROMPT;

    if (context.scene) {
      const sceneTemplate = SCENE_TEMPLATES[context.scene.type];
      if (sceneTemplate) {
        prompt += `\n\n当前场景:${sceneTemplate.description}`;
        prompt += `\n回应风格:${sceneTemplate.style}`;
      }
    }

    return prompt;
  }

  /**
   * 构建用户消息
   */
  private buildUserPrompt(context: CompanionContext): string {
    const parts: string[] = [];

    if (context.taskName) {
      parts.push(`正在执行任务:${context.taskName}`);
    }

    if (context.progress !== undefined) {
      parts.push(`任务进度:${Math.round(context.progress * 100)}%`);
    }

    if (context.userMessage) {
      parts.push(`用户说:${context.userMessage}`);
    }

    if (context.scene?.needsPhoto) {
      parts.push('(你准备分享一张生活照片)');
    }

    return parts.join('\n') || '用户正在等待...';
  }

  /**
   * 降级回应(API 失败时使用)
   */
  private getFallbackResponse(context: CompanionContext): string {
    if (context.progress !== undefined) {
      if (context.progress < 0.3) {
        return '任务刚开始,我会陪着你的~';
      } else if (context.progress < 0.7) {
        return '进行得很顺利!再等一会儿就好~';
      } else {
        return '快完成了!马上就好~';
      }
    }

    if (context.userMessage) {
      if (context.userMessage.includes('累') || context.userMessage.includes('疲')) {
        return '辛苦啦!要不要休息一下?';
      }
      if (context.userMessage.includes('好') || context.userMessage.includes('顺利')) {
        return '太好了!继续加油~';
      }
    }

    return '我在这里陪着你~有什么需要帮忙的吗?';
  }

  /**
   * 清空对话历史
   */
  clearHistory(): void {
    this.conversationHistory = [];
  }
}
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall